import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); boolean[][] field = new boolean[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = (sc.nextInt() == 1); } } int q = sc.nextInt(); ArrayDeque deq = new ArrayDeque<>(); for (int i = 0; i < q; i++) { int r = sc.nextInt() - 1; int c = sc.nextInt() - 1; boolean color = (sc.nextInt() == 1); deq.add(new Path(r, c, color)); while (deq.size() > 0) { Path p = deq.poll(); if (p.r < 0 || p.r >= h || p.c < 0 || p.c >= w || field[p.r][p.c] == p.color) { continue; } field[p.r][p.c] = p.color; deq.add(new Path(p.r - 1, p.c, p.color)); deq.add(new Path(p.r + 1, p.c, p.color)); deq.add(new Path(p.r, p.c - 1, p.color)); deq.add(new Path(p.r, p.c + 1, p.color)); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j > 0) { sb.append(" "); } if (field[i][j]) { sb.append("1"); } else { sb.append("0"); } } sb.append("\n"); } System.out.print(sb); } static class Path { int r; int c; boolean color; public Path(int r, int c, boolean color) { this.r = r; this.c = c; this.color = color; } } }