import sys input = sys.stdin.readline H,W=map(int,input().split()) MAP=[list(map(int,input().split())) for i in range(H)] flag=0 Q=int(input()) for tests in range(Q): x,y,c=map(int,input().split()) x-=1 y-=1 if flag==1: MAP[0][0]=c continue if MAP[x][y]==c: continue Q=[(x,y)] MAP[x][y]=c count=1 while Q: x,y=Q.pop() for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]: if 0<=z