import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Iterator; import java.util.Queue; public class Main_yukicoder307 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); int[] dr = {-1, 1, 0, 0}; int[] dc = {0, 0, -1, 1}; int h = sc.nextInt(); int w = sc.nextInt(); int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { a[i][j] = sc.nextInt(); } } int q = sc.nextInt(); for (int tcase = 0; tcase < q; tcase++) { int r = sc.nextInt() - 1; int c = sc.nextInt() - 1; int x = sc.nextInt(); if (x == a[r][c]) { continue; } int cp = a[r][c]; Queue qr = new ArrayDeque(); Queue qc = new ArrayDeque(); qr.add(r); qc.add(c); a[r][c] = x; while (!qr.isEmpty()) { int cr = qr.remove(); int cc = qc.remove(); for (int i = 0; i < dr.length; i++) { int tmpr = cr + dr[i]; int tmpc = cc + dc[i]; if (tmpr < 0 || tmpr >= h || tmpc < 0 || tmpc >= w) { continue; } if (a[tmpr][tmpc] != cp) { continue; } a[tmpr][tmpc] = x; qr.add(tmpr); qc.add(tmpc); } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j == 0) { pr.printf("%d", a[i][j]); } else { pr.printf(" %d", a[i][j]); } } pr.println(""); } pr.close(); sc.close(); } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }