import sys sys.setrecursionlimit(10 ** 6) def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] dij = [(0, 1), (1, 0), (0, -1), (-1, 0)] def main(): def bfs(si, sj, x): pos=0 stack[pos]=(si,sj) res=0 while pos>=0: i,j=stack[pos] res+=1 pos-=1 for di, dj in dij: ni, nj = i + di, j + dj if ni < 0 or nj < 0 or ni >= h or nj >= w: continue if aa[ni][nj] == x: continue aa[ni][nj] = x pos+=1 stack[pos]=(ni,nj) return res h, w = MI() aa = LLI(h) stack=[() for _ in range(h*w)] q=II() rcx=LLI(q) for r,c,x in rcx: r, c = r - 1, c - 1 if aa[r][c] == x: continue aa[r][c] = x if bfs(r, c, x)==h*w: x=rcx[-1][-1] for i in range(h): aa[i]=[x]*w break for row in aa:print(*row) main()