from collections import deque h, w = map(int, input().split()) grid = [list(map(int, input().split())) for _ in range(h)] dy = [-1, 1, 0, 0] dx = [0, 0, -1, 1] Q = int(input()) for _ in range(Q): r, c, x = map(int, input().split()) r -= 1 c -= 1 original = grid[r][c] if original == x: continue q = deque() q.append((r, c)) grid[r][c] = x while q: cy, cx = q.popleft() for i in range(4): ny = cy + dy[i] nx = cx + dx[i] if 0 <= ny < h and 0 <= nx < w: if grid[ny][nx] == original: grid[ny][nx] = x q.append((ny, nx)) for row in grid: print(*row)