#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines H, W = map(int, readline().split()) A = [] for _ in range(H): A += list(map(int, readline().split())) Q = int(readline()) m = map(int, read().split()) RCX = zip(m, m, m) class UnionFind: def __init__(self, N): self.root = list(range(N)) self.size = [1] * (N) self.component_border = [[i] for i in range(N)] def find_root(self, x): root = self.root while root[x] != x: root[x] = root[root[x]] x = root[x] return x def merge(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return False assert A[x] == A[y] sx, sy = self.size[x], self.size[y] if sx > sy: sx, sy = sy, sx x, y = y, x self.root[y] = x self.size[x] += sy self.component_border[x] += self.component_border[y] self.component_border[y].clear() return True N = H * W uf = UnionFind(N) find = uf.find_root merge = uf.merge for i in range(N): x, y = divmod(i, W) if y and A[i - 1] == A[i]: merge(i - 1, i) if x and A[i - W] == A[i]: merge(i - W, i) def paint(i, c): i = find(i) if A[i] == c: return A[i] ^= 1 border = uf.component_border[i].copy() uf.component_border[i].clear() for v in border: x, y = divmod(v, W) if 0 < y: merge(v - 1, v) if y < W - 1: merge(v + 1, v) if 0 < x: merge(v - W, v) if x < H - 1: merge(v + W, v) i = find(i) for R, C, X in RCX: R -= 1 C -= 1 i = R * W + C paint(i, X) A = [A[find(i)] for i in range(N)] rows = (A[i: i + W] for i in range(0, N, W)) print('\n'.join(' '.join(map(str, row)) for row in rows))