#include "math.h" #include #include #include #include #include #include #include #include #include #include #define ifor(i, a, b) for (int i = (a); i < (b); i++) #define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--) #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) using namespace std; typedef long double ld; typedef long long int lli; typedef complex P; const double eps = 1e-11; int vx[4] = {1, 0, -1, 0}; int vy[4] = {0, 1, 0, -1}; typedef vector Vec; typedef vector vec; typedef vector MAT; typedef vector mat; lli MOD = 1000000007; int h, w; bool inrange(int x, int y) { return 0 <= x && x < h && y < w && 0 <= y; } int f[205][205]; bool used[300][300]; int temp[300][300]; void dfs(int x, int y, int from, int c) { temp[x][y] = c; used[x][y] = true; rep(i, 4) { int nx = vx[i] + x; int ny = vy[i] + y; if (inrange(nx, ny)) { if (f[nx][ny] == from && !used[nx][ny]) { temp[x][y] = c; used[x][y] = true; dfs(nx, ny, from, c); } } } }; int main() { cin >> h >> w; int a, b, c; rep(i, h) rep(j, w) scanf("%d", &f[i][j]); int q; cin >> q; bool mono = false; bool update; int col; rep(m, q) { scanf("%d%d%d", &a, &b, &c); a--, b--; if (!mono) { int color = f[0][0]; update = false; rep(i, h) rep(j, w) { used[i][j] = false; temp[i][j] = f[i][j]; if (f[0][0] != f[i][j]) update = true; } if (!update) { mono = true; } dfs(a, b, f[a][b], c); rep(i, h) rep(j, w) f[i][j] = temp[i][j]; } else { col = c; } // rep(i, h) rep(j, w) // { // cout << f[i][j]; // if (j == w - 1) // cout << endl; // else // cout << ' '; // } // cout << endl; } if (mono) rep(i, h) rep(j, w) f[i][j] = col; rep(i, h) rep(j, w) { cout << f[i][j]; if (j == w - 1) cout << endl; else cout << ' '; } }