#include #include #include #include template std::vector vec(int len, T elem) { return std::vector(len, elem); } const std::vector> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; void solve() { int h, w; std::cin >> h >> w; auto ss = vec(h, vec(w, 0)); for (auto& s : ss) { for (auto& x : s) std::cin >> x; } bool allsame = false; int q; std::cin >> q; while (q--) { int sx, sy, c; std::cin >> sx >> sy >> c; if (allsame) { ss[0][0] = c; continue; } if (ss[--sx][--sy] == c) continue; std::queue> que; que.emplace(sx, sy); ss[sx][sy] = c; while (!que.empty()) { auto [x, y] = que.front(); que.pop(); for (auto [dx, dy] : dxys) { int nx = x + dx, ny = y + dy; if (nx < 0 || h <= nx || ny < 0 || w <= ny || ss[nx][ny] == c) continue; ss[nx][ny] = c; que.emplace(nx, ny); } } bool judge = true; for (const auto& s : ss) { for (auto x : s) { if (x != ss[0][0]) judge = false; } } if (judge) allsame = true; } for (const auto& s : ss) { for (auto x : s) std::cout << (allsame ? ss[0][0] : x) << " "; std::cout << "\n"; } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }