#import time #start = time.clock() h,w = map(int, raw_input().split()) a = [] for i in xrange(h): a.append( map(int, raw_input().split()) ) q_ = [None] * (h*w) ptr = 0 def bfs (start, color) : global a global q_ global ptr if a[start // w][start % w] == color : return 0 dx = [0,0,1,-1] dy = [1,-1,0,0] ret = 0 q_[ptr] = start ptr += 1 a[start // w][start % w] = color while ptr != 0 : pos = q_[ptr-1] ptr -= 1 x = pos % w y = pos // w ret += 1; for k in range(4) : xx = x + dx[k] yy = y + dy[k] next_pos = yy * w + xx if xx<0 or xx >= w or yy<0 or yy>=h : continue if (a[yy][xx] == color) : continue a[yy][xx] = color q_[ptr] = next_pos ptr += 1 return ret; filled = False; last = -1; q = input() for i in xrange(q): r,c,x = map(int, raw_input().split()) r = r-1; c = c-1; last = x; if (a[r][c] == x or filled == True) : continue cnt = bfs(r * w + c, x) if (w*h == cnt) : filled = True #import sys #print >> sys.stderr, filled if (filled == True) : for i in xrange(h): a[i] = str(last) * w for i in xrange(h): print ( ' '.join(map(str, a[i])) ) #print >> sys.stderr, time.clock() - start , "sec"