// @author godmar import java.util.*; import java.util.function.*; import java.io.*; public class BackupTowersGodmar { static record Entry(int i, int j, int closest) { } static record Tower(int i, int j) { } @FunctionalInterface interface QuadFunction { R apply(A a, B b, C c, D d); } public static void main(String []av) throws IOException { var sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int t = sc.nextInt(); int [][] closest = new int[h][w]; int [][] secondclosest = new int[h][w]; Tower [] towers = new Tower[t]; Deque bfs1 = new ArrayDeque<>(); Deque bfs2 = new ArrayDeque<>(); for (int i = 0; i < t; i++) { int rr = sc.nextInt() - 1; int cc = sc.nextInt() - 1; towers[i] = new Tower(rr, cc); closest[rr][cc] = i+1; bfs1.addLast(new Entry(rr, cc, i+1)); } QuadFunction isCloser = (i, j, a, b) -> { int adist = Math.abs(i - towers[a-1].i) + Math.abs(j - towers[a-1].j); int bdist = Math.abs(i - towers[b-1].i) + Math.abs(j - towers[b-1].j); int rc = Integer.compare(adist, bdist); return (rc != 0 ? rc : Integer.compare(a, b)) < 0; }; while (!bfs1.isEmpty()) { var e = bfs1.removeFirst(); for (int []d : new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }}) { int ni = e.i + d[0]; int nj = e.j + d[1]; if (0 <= ni && ni < h && 0 <= nj && nj < w) { if (closest[ni][nj] == 0) { closest[ni][nj] = e.closest; bfs1.addLast(new Entry(ni, nj, e.closest)); } else if (closest[ni][nj] != e.closest) { if (secondclosest[ni][nj] == 0) { secondclosest[ni][nj] = e.closest; bfs2.addLast(new Entry(ni, nj, e.closest)); } } } } } while (!bfs2.isEmpty()) { var e = bfs2.removeFirst(); for (int []d : new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }}) { int ni = e.i + d[0]; int nj = e.j + d[1]; if (0 <= ni && ni < h && 0 <= nj && nj < w) { if (closest[ni][nj] != e.closest) { if (secondclosest[ni][nj] == 0 || isCloser.apply(ni, nj, e.closest, secondclosest[ni][nj])) { secondclosest[ni][nj] = e.closest; bfs2.addLast(new Entry(ni, nj, e.closest)); } } } } } output(closest); output(secondclosest); } static void output(int [][]closest) { int h = closest.length; int w = closest[0].length; PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { pw.print(closest[i][j] + (j < w-1 ? " " : "")); } pw.println(); } pw.flush(); } // ---------- Fast Scanner ---------- // Courtesy GPT-5 static class Scanner { private final InputStream in; private final byte[] buffer = new byte[1 << 16]; private int ptr = 0, len = 0; Scanner(InputStream is) { in = is; } private int read() throws IOException { if (ptr >= len) { len = in.read(buffer); ptr = 0; if (len <= 0) return -1; } return buffer[ptr++]; } long nextLong() throws IOException { int c; do { c = read(); } while (c <= ' ' && c != -1); long sgn = 1; if (c == '-') { sgn = -1; c = read(); } long val = 0; while (c > ' ') { val = val * 10 + (c - '0'); c = read(); } return val * sgn; } int nextInt() throws IOException { return (int) nextLong(); } } }