from collections import deque import sys input = sys.stdin.readline 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] def bfs(r, c, x): original = grid[r][c] if original == x: return 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)) Q = int(input()) for _ in range(Q): r, c, x = map(int, input().split()) bfs(r - 1, c - 1, x) for row in grid: print(*row)