#include #include #include #include #include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define var auto #define mod 1000000007 #define inf 2147483647 #define nil -1 typedef long long ll; using namespace std; inline int input(){ int a; cin >> a; return a; } template inline void output(T a, int p) { if(p){ cout << fixed << setprecision(p) << a << "\n"; } else{ cout << a << "\n"; } } // end of template int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { cin.tie(0); // source code int H = input(); int W = input(); vector> A(H, vector(W)); rep(i, H){ rep(j, W){ cin >> A[i][j]; } } int Q; cin >> Q; queue> q; vector R(Q), C(Q), X(Q); rep(i, Q){ cin >> R[i] >> C[i] >> X[i]; R[i]--, C[i]--; } int count = 0; rep(i, Q){ if (A[R[i]][C[i]] == X[i]) continue; // A[R][C] != X q.push(make_pair(R[i], C[i])); A[R[i]][C[i]] = X[i]; count = 0; while (!q.empty()) { pair t = q.front(); q.pop(); count++; rep(j, 4){ if (t.first + dy[j] >= 0 && t.first + dy[j] < H && t.second + dx[j] >= 0 && t.second + dx[j] < W && A[t.first + dy[j]][t.second + dx[j]] != X[i]) { // 隣接している,かつXと違う色のマスを訪れる A[t.first + dy[j]][t.second + dx[j]] = X[i]; q.push(make_pair(t.first + dy[j], t.second + dx[j])); } } } if (count == H * W) { break; } } if(count < H * W){ rep(i, H){ rep(j, W){ if (j) cout << " "; cout << A[i][j]; } cout << endl; } } else{ rep(i, H){ rep(j, W){ if (j) cout << " "; cout << X[Q - 1]; } cout << endl; } } return 0; }