#include #define rep(i,a,n) for(int i=a;i=b;i--) #define all(a) a.begin(),a.end() #define o(a) cout< vi; typedef vector vvi; typedef pair pii; int a[210][210]; int dy[4]={1,-1,0,0}; int dx[4]={0,0,1,-1}; int h,w,q; bool f[210][210]; void dfs(int r,int c,int color,int x){ f[r][c]=true; a[r][c]=x; rep(i,0,4){ int ny=r+dy[i]; int nx=c+dx[i]; if(0<=ny && ny>h>>w; rep(i,0,h) rep(j,0,w) cin>>a[i][j]; cin>>q; vi r(q),c(q),x(q); rep(i,0,q){ scanf("%d %d %d",&r[i],&c[i],&x[i]); } bool g=false; rep(i,0,q){ r[i]--; c[i]--; int color=a[r[i]][c[i]]; rep(k,0,h) rep(j,0,w) f[k][j]=false; dfs(r[i],c[i],color,x[i]); int sum=0; rep(k,0,h) rep(j,0,w) if(a[k][j]==a[0][0]) sum++; if(sum==h*w){ g=true; break; } } if(g){ rep(i,0,h) rep(j,0,w) a[i][j]=x[q-1]; } rep(i,0,h){ rep(j,0,w){ cout<<(j?" ":"")<