import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); boolean[][] field = new boolean[h][w]; UnionFindTree uft = new UnionFindTree(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = (sc.nextInt() == 1); uft.color[i * w + j] = field[i][j]; if (j > 0 && field[i][j] == field[i][j - 1]) { uft.unite(i * w + j, i * w + j - 1); } if (i > 0 && field[i][j] == field[i - 1][j]) { uft.unite(i * w + j, (i - 1) * w + j); } } } uft.prepare(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j > 0 && field[i][j] != field[i][j - 1]) { uft.next(i * w + j, i * w + j - 1); } if (i > 0 && field[i][j] != field[i - 1][j]) { uft.next(i * w + j, (i - 1) * w + j); } } } int q = sc.nextInt(); while (q-- > 0) { int idx = (sc.nextInt() - 1) * w + sc.nextInt() - 1; boolean isBlack = (sc.nextInt() == 1); if (uft.getColor(idx) == isBlack) { continue; } uft.setColor(idx, isBlack); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j > 0) { sb.append(" "); } if (uft.getColor(i * w + j)) { sb.append("1"); } else { sb.append("0"); } } sb.append("\n"); } System.out.print(sb); } static class UnionFindTree { int[] parents; boolean[] color; HashMap> nextTo = new HashMap<>(); public UnionFindTree(int size) { parents = new int[size]; color = new boolean[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(x)] = find(y); } } public boolean getColor(int x) { return color[find(x)]; } public void prepare() { for (int i = 0; i < parents.length; i++) { if (i == find(i)) { nextTo.put(i, new HashSet<>()); } } } public void next(int x, int y) { int xx = find(x); int yy = find(y); nextTo.get(xx).add(yy); nextTo.get(yy).add(xx); } public void setColor(int x, boolean c) { int xx = find(x); color[xx] = c; HashSet newNext = new HashSet<>(); for (int y : nextTo.get(xx)) { unite(y, xx); for (int z : nextTo.get(y)) { if (find(z) != xx) { newNext.add(find(z)); } } } nextTo.put(xx, newNext); } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }