#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,int sum){ f[r][c]=true; sum+=x-color; a[r][c]=x; rep(i,0,4){ int ny=r+dy[i]; int nx=c+dx[i]; if(0<=ny && ny>h>>w; int sum=0; rep(i,0,h) rep(j,0,w){ cin>>a[i][j]; sum+=a[i][j]; } cin>>q; vi r(q),c(q),x(q); rep(i,0,q){ cin>>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],sum); if(sum==h*w||sum==0){ 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?" ":"")<