#include using namespace std; #define INF 1.1e9 #define LINF 1.1e18 #define FOR(i,a,b) for (int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define pb push_back #define pf push_front #define fi first #define se second #define BIT(x,n) bitset(x) #define PI 3.14159265358979323846 typedef long long ll; typedef pair P; typedef pair PP; //----------------------------------------------------------------------------- int h,w,fld[200][200],r,c,x,q; int bfs(int sx,int sy,int color,int dir) { int cnt=1; bool used[200][200]; REP(i,200) REP(j,200) used[i][j]=false; using P=pair; queue

Q; int S=fld[sy][sx]; fld[sy][sx]=color; used[sy][sx]=true; Q.emplace(sx,sy); int dx[]={ 0, 0, 1,-1, 1,-1, 1,-1}; int dy[]={ 1,-1, 0, 0, 1, 1,-1,-1}; while(!Q.empty()) { P p=Q.front();Q.pop(); for(int i=0;i=w||ny<0||ny>=h) continue; if(fld[ny][nx]!=S) continue; if(used[ny][nx]) continue; used[ny][nx]=true; fld[ny][nx]=color; cnt++; Q.emplace(nx,ny); } } return cnt; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin>>h>>w; REP(i,h) REP(j,w) cin>>fld[i][j]; cin>>q; bool ok=false; while(q--) { cin>>r>>c>>x; if(ok) { if(q==0) { REP(i,h) REP(j,w) fld[i][j]=x; } } else { if(bfs(c-1,r-1,x,4)==h*w) ok=true; } } REP(i,h) { REP(j,w) cout<