#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; int main(){ int h, w; cin >> h >> w; vector> a(h, vector(w)); rep(i,h) rep(j,w) cin >> a[i][j]; int q, cnt = 0; cin >> q; int allc = -1; while (q--) { int r, c, x; cin >> r >> c >> x; r--; c--; if (cnt == h*w || cnt == 0) { allc = x; continue; } if (a[r][c] == x) continue; queue

que; que.push(make_pair(r, c)); while (!que.empty()) { auto p = que.front(); que.pop(); if (a[p.first][p.second] == x) continue; a[p.first][p.second] = x; if (x == 0) cnt--; else cnt++; for (int i = 0; i < 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue; if (a[nx][ny] == x) continue; que.push(make_pair(nx,ny)); } } } rep(i,h) { rep(j,w) cout << (allc != -1 ? allc : a[i][j]) << " "; cout << endl; } return 0; }