#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]; if(color == x) continue; a[r][c] = x; int cnt = 0; while(!que.empty()){ // cnt++; 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 && ny < h && 0 <= nx && nx < w)) continue; if(a[ny][nx] == color && a[ny][nx] != x){ 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; }