// yukicoder - - 307 / 2016-03-01 #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair pll; typedef ll int__; #define rep(i,j) for(int__ i=0;i<(int__)(j);i++) #define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++) #define all(v) v.begin(),v.end() templateostream& operator << (ostream &os , const vector &v){ rep(i,v.size()) os << v[i] << (i!=v.size()-1 ? " " : "\n"); return os; } templateistream& operator >> (istream &is , vector &v){ rep(i,v.size()) is >> v[i]; return is; } #ifdef DEBUG void debug(){ cerr << endl; } #endif template void debug(const F &car,const R&... cdr){ #ifdef DEBUG cerr << car << " "; debug(cdr...); #endif } const int dx[] = { 0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int H, W; vector> G; void spread(int x, int y, int nxt_color){ int prev_color = G[y][x]; if(prev_color == nxt_color) return; G[y][x] = nxt_color; rep(dir, 4){ int nx = x + dx[dir]; int ny = y + dy[dir]; if(0 <= nx and nx < W and 0 <= ny and ny < H){ if(G[ny][nx] == prev_color){ spread(nx, ny, nxt_color); } } } } bool solve(){ cin >> H >> W; G.resize(H, vector(W)); rep(i, H) cin >> G[i]; int Q; cin >> Q; rep(i, Q){ int y, x, c; cin >> y >> x >> c; y--; x--; spread(x, y, c); } rep(y, H){ rep(x, W){ cout << G[y][x] << (x != W-1 ? " " : "\n"); } } return false; } int main() { ios::sync_with_stdio(false); while(solve()); return 0; }