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]; int tCount = 0; int fCount = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = (sc.nextInt() == 1); if (field[i][j]) { tCount++; } else { fCount++; } } } ArrayDeque deq = new ArrayDeque<>(); int q = sc.nextInt(); boolean last = true; for (int i = 0; i < q; i++) { if (tCount > 0 && fCount > 0) { deq.add(new Path(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt() == 1)); } else { sc.nextInt(); sc.nextInt(); last = (sc.nextInt() == 1); } while (deq.size() > 0) { Path p = deq.poll(); if (p.row < 0 || p.row >= h || p.col < 0 || p.col >= w) { continue; } if (field[p.row][p.col] == p.type) { continue; } field[p.row][p.col] = p.type; last = p.type; if (p.type) { tCount++; fCount--; } else { tCount--; fCount++; } deq.add(new Path(p.row - 1, p.col, p.type)); deq.add(new Path(p.row + 1, p.col, p.type)); deq.add(new Path(p.row, p.col - 1, p.type)); deq.add(new Path(p.row, p.col + 1, p.type)); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j > 0) { sb.append(" "); } if (tCount > 0 && fCount > 0) { if (field[i][j]) { sb.append("1"); } else { sb.append("0"); } } else { if (last) { sb.append("1"); } else { sb.append("0"); } } } sb.append("\n"); } System.out.print(sb); } static class Path { int row; int col; boolean type; public Path(int row, int col, boolean type) { this.row = row; this.col = col; this.type = type; } } }