import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines h, w=map(int, readline().split()) a=[] cnt1=0 for i in range(h): ai=list(map(int, readline().split())) a.append(ai) cnt1+=sum(ai) a0=-1 if cnt1==0 or cnt1==h*w: a0=0 q=int(readline()) for _ in range(q): r, c, x=map(int, readline().split()) r-=1 c-=1 if a0!=-1: if (a[r][c]^a0)!=x: a0^=1 continue if a[r][c]==x: continue que=[(r, c)] a[r][c]=x if x==1: cnt1+=1 else: cnt1-=1 for p in que: r0, c0=p[0], p[1] for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: r1, c1=r0+dx, c0+dy if r1<0 or r1>=h or c1<0 or c1>=w or a[r1][c1]==x: continue a[r1][c1]=x if x==1: cnt1+=1 else: cnt1-=1 que.append((r1, c1)) if cnt1==0 or cnt1==h*w: a0=0 if a0==-1:a0=0 for i in range(h): print(' '.join(map(str, [x^a0 for x in a[i]])))