#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; const int DY[] = {-1,0,1,0}; const int DX[] = {0,1,0,-1}; int H, W, A[200][200], cnt[2]; void dfs(int y, int x, int col){ if(A[y][x] != col){ A[y][x] = col; cnt[col]++; cnt[1 - col]--; rep(i, 4){ int ny = y + DY[i], nx = x + DX[i]; if(ny >= 0 && ny < H&&nx >= 0 && nx < W)dfs(ny, nx, col); } } } int main(){ while(cin >> H >> W){ cnt[0] = cnt[1] = 0; rep(y, H)rep(x, W)scanf("%d", &A[y][x]), cnt[A[y][x]]++; int lastCol = 0; int Q; cin >> Q; while(Q--){ int Y, X, C; scanf("%d%d%d", &Y, &X, &C); --Y; --X; lastCol = C; if(cnt[0] == 0 || cnt[1] == 0)continue; dfs(Y, X, C); } if(cnt[0]*cnt[1]==0)rep(y, H)rep(x, W)A[y][x] = lastCol; rep(y, H){ rep(x, W){ printf("%d%c", A[y][x], x == W - 1 ? '\n' : ' '); } } } }