#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 = input(); int count = 0; // 盤面が変化した回数 queue> q; for (int i = 0; i < Q && count <= H + W; i++) { int R, C, X; cin >> R >> C >> X; R--, C--; if (A[R][C] == X) continue; q.push(make_pair(R, C)); while (!q.empty()) { var t = q.front(); q.pop(); A[t.first][t.second] = X; 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) { q.push(make_pair(t.first + dy[j], t.second + dx[j])); } } } count++; } rep(i, H){ rep(j, W){ if (j) cout << " "; cout << A[i][j]; } cout << endl; } return 0; }