#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int dy[] = {0, 0, 1, -1}; const int dx[] = {1, -1, 0, 0}; int main() { int h, w; cin >> h >> w; vector > a(h+2, vector(w+2, -1)); vector cnt(2, 0); for(int y=1; y<=h; ++y){ for(int x=1; x<=w; ++x){ cin >> a[y][x]; ++ cnt[a[y][x]]; } } int query; cin >> query; while(--query >= 0){ int y, x, b; cin >> y >> x >> b; if(a[y][x] == b) continue; if(cnt[0] == h * w || cnt[1] == h * w){ if(query == 0){ for(int y=1; y<=h; ++y){ for(int x=1; x<=w; ++x){ a[y][x] = b; } } } continue; } queue > q; q.push(make_pair(y, x)); a[y][x] = b; ++ cnt[b]; -- cnt[b^1]; while(!q.empty()){ int y2 = q.front().first; int x2 = q.front().second; q.pop(); for(int i=0; i<4; ++i){ int y3 = y2 + dy[i]; int x3 = x2 + dx[i]; if(a[y3][x3] == (b ^ 1)){ q.push(make_pair(y3, x3)); a[y3][x3] = b; ++ cnt[b]; -- cnt[b^1]; } } } } for(int y=1; y<=h; ++y){ cout << a[y][1]; for(int x=2; x<=w; ++x){ cout << ' ' << a[y][x]; } cout << endl; } return 0; }