#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; bool flag = false; int fcolor; rep(i, q){ int r, c, x; cin >> r >> c >> x; r--;c--; if(flag){ fcolor = x; continue; } 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()){ 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; cnt++; que.push(make_pair(ny, nx)); } } } if(cnt >= h * w - 1){ flag = true; } } if(flag){ rep(i, h){ rep(j, w){ printf("%d ", fcolor); } printf("\n"); } }else{ rep(i, h){ rep(j, w){ printf("%d ", a[i][j]); } printf("\n"); } } return 0; }