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()) ) visit = [None] * (h*w) q_ = [None] * (h*w) ptr = 0 def bfs (start, color, turn) : global a global q_ global ptr global visit 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 visit[start] = turn while ptr != 0 : pos = q_[ptr-1] ptr -= 1 x = pos % w y = pos // w ret += 1; a[y][x] = color for k in xrange(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 (visit[next_pos] == turn) or (a[yy][xx] == color) : continue visit[next_pos] = turn 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, i) 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 , "ms"