#include using namespace std; typedef long long ll; typedef vector vi; typedef vector vl; typedef complex P; typedef pair pii; #define REP(i,n) for(ll i=0;i=w || y>=h)return; if(used[y][x])return; used[y][x] = true; if(mp[y][x] == col)return; nxt[y][x] = col; REP(i,4){ dfs(y+vy[i],x+vx[i],col); } } int main(){ cin >> h >> w; // vector mp(h,vi(w)); REP(i,h)REP(j,w){ cin>>mp[i][j]; nxt[i][j] = mp[i][j]; } int q; cin >> q; REP(_,q){ int y,x,col; cin >> y >> x >> col; --y;--x; fill(used[0],used[200],false); dfs(y,x,col); if(used[0][0] && used[h-1][0] && used[0][w-1] && used[h-1][w-1]){ REP(t,q-1-_){ cin >> y >> x >> col; } fill(mp[0],mp[200],col); break; } REP(i,h)REP(j,w)mp[i][j]=nxt[i][j]; } REP(i,h){ cout << mp[i][0]; REPR(j,w) cout << " " << mp[i][j]; cout << endl; } return 0; }