#include #include #include #include //#include #include #include #include #include #include //#include #include #include #include //#include #include #include //#include #include #include #include #include const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef vector vi; typedef vector vll; typedef pair pii; const int MAX = 222; int A[MAX][MAX]; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> A[i][j]; } int Q; cin >> Q; bool done = false; int all = -1; for (int i = 0; i < Q; i++) { int r, c, x; cin >> r >> c >> x; r--; c--; if (done) { all = x; continue; } if (A[r][c] != x) { queue que; int origin = A[r][c]; que.push(pii(r, c)); int cnt = 0; while (!que.empty()) { cnt++; pii p = que.front(); que.pop(); A[p.first][p.second] = x; for (int k = 0; k < 4; k++) { int ny = p.first+dy[k]; int nx = p.second+dx[k]; if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue; if (A[ny][nx] == origin) { que.push(pii(ny, nx)); A[ny][nx] = x; } } } if (cnt >= H*W) done = true; } } if (done) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << all; if (j < W-1) cout << " "; } cout << endl; } } else { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << A[i][j]; if (j < W-1) cout << " "; } cout << endl; } } return 0; }