//https://ncode.syosetu.com/n4830bu/307/ #include using namespace std; int main() { int H, W; cin >> H >> W; vector A(H, vector(W, 0)); for (auto&& V : A) { for (auto&& a : V) { cin >> a; } } int Q; cin >> Q; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int clr = -1; bool maine = false; vector cnt(H, vector(W, 0)); while (Q--) { int R, C, X; cin >> R >> C >> X; R--; C--; if (maine) { clr = X; continue; } if (A[R][C] == X) continue; vector seen(H, vector(W, false)); queue> que; que.push({R, C}); seen[R][C] = true; while (!que.empty()) { auto [h, w] = que.front(); cnt[h][w]++; que.pop(); for (int i = 0; i < 4; i++) { int h_nxt = h + dx[i], w_nxt = w + dy[i]; if (h_nxt >= 0 && h_nxt < H && w_nxt >= 0 && w_nxt < W) { if (!seen[h_nxt][w_nxt] && A[h_nxt][w_nxt] != X) { seen[h_nxt][w_nxt] = true; que.push({h_nxt, w_nxt}); } } } } for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { if (seen[h][w]) A[h][w] = X; if (cnt[h][w] > H + W) maine = true; } } } for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { cout << (clr == -1 ? A[h][w] : clr) << " \n"[w == W - 1]; } } }