#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //define #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; #define dump(x) cerr << #x << " = " << (x) << endl; #define INF (INT_MAX/2) #define PI (2*acos(0.0)) #define EPS (1e-8) #define REP(i,a,b) for(int i=(a); i<(b);++i) #define rep(i,n) REP(i,0,n) typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair pll; typedef vector vint; typedef vector > vvint; typedef vector vll; typedef vector > vvll; int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int H, W; int cnt_zero; vvint A; void dfs(int r, int c, int x){ int color = A[r][c]; A[r][c] = x; if(color == 0 && A[r][c] == 1) cnt_zero--; if(color == 1 && A[r][c] == 0) cnt_zero++; for(int i = 0; i < 4; i++){ int nr = r + dy[i]; int nc = c + dx[i]; if(0 <= nr && nr < H && 0 <= nc && nc < W && A[nr][nc] == color && A[nr][nc] != x){ dfs(nr, nc, x); } } } int main(void){ ios_base::sync_with_stdio(0); cin >> H >> W; A = vvint(H); int input; rep(i, H){ rep(j, W){ cin >> input; if(input == 0) cnt_zero++; A[i].push_back(input); } } int Q, R, C, X; int all = -1; cin >> Q; rep(i, Q){ cin >> R >> C >> X; R--; C--; if(cnt_zero != H * W && cnt_zero != 0) dfs(R, C, X); else all = X; } if(all == -1){ 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 << all; } cout << endl; } } return 0; }