#include #include #include #include #include using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; int h, w; int a[210][210]; int q; int main(void){ cin >> h >> w; rep(i, h)rep(j, w) cin >> a[i][j]; cin >> q; rep(i, q){ int r, c, x; cin >> r >> c >> x; r--;c--; queue > que; que.push(make_pair(r, c)); int color = a[r][c]; a[r][c] = x; while(!que.empty()){ int nowy = que.front().first, nowx = que.front().second; que.pop(); rep(k, 4){ int ny = nowy + dy[k], nx = nowx + dx[k]; if(!(0 <= ny < h && 0 <= nx && nx < w)) continue; if(a[ny][nx] == color){ a[ny][nx] = x; que.push(make_pair(ny, nx)); } } } } rep(i, h){ rep(j, w){ printf("%d ", a[i][j]); } printf("\n"); } return 0; }