#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int g_visited[210][210]; int g_vid; int A[210][210]; const int DY[4] = {-1, 0, 1, 0}; const int DX[4] = {0, 1, 0, -1}; int fill_x(int y, int x, int c, int nc) { int cnt = 1; g_visited[y][x] = g_vid; A[y][x] = nc; for (int dir = 0; dir < 4; ++dir) { int ny = y + DY[dir]; int nx = x + DX[dir]; if (A[ny][nx] != c) continue; cnt += fill_x(ny, nx, c, nc); } return cnt; } int main() { g_vid = 0; memset(g_visited, -1, sizeof(g_visited)); int H, W; cin >> H >> W; memset(A, -1, sizeof(A)); for (int y = 1; y <= H; ++y) { for (int x = 1; x <= W; ++x) { cin >> A[y][x]; } } int Q; cin >> Q; int max_cnt = 0; int last_color = -1; for (int i = 0; i < Q; ++i) { int R, C, X; cin >> R >> C >> X; if (max_cnt == H * W) { last_color = X; continue; } if (A[R][C] == X) continue; int cnt = fill_x(R, C, A[R][C], X); max_cnt = max(max_cnt, cnt); } for (int y = 1; y <= H; ++y) { for (int x = 1; x <= W; ++x) { if (last_color != -1) { cout << last_color; } else { cout << A[y][x]; } if (x < W) cout << " "; } cout << endl; } return 0; }