import sys from collections import deque input = sys.stdin.buffer.readline H, W = map(int, input().split()) table = list(list(map(int, input().split())) for _ in range(H)) flag_all = False final_color = 0 Q = int(input()) for _ in range(Q): R, C, X = map(int, input().split()) R -= 1 C -= 1 if table[R][C] == X: continue if flag_all: final_color = X else: table[R][C] = X d = deque([(R, C)]) ct = 0 while d: r, c = d.pop() ct += 1 for dr, dc in ((1, 0), (0, 1), (-1, 0), (0, -1)): rdr = r + dr cdc = c + dc if 0 <= rdr < H and 0 <= cdc < W and table[rdr][cdc] != X: table[rdr][cdc] = X d.append((rdr, cdc)) if ct == H * W: flag_all = True final_color = X if flag_all: for _ in range(H): print(*([final_color] * W)) else: for row in table: print(*row)