#include using namespace std; //typedef //------------------------------------------ typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef long long LL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) //constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); int dx[] = {-1,1,0,0}; int dy[] = {0,0,-1,1}; int col[200][200]; bool use[200][200]; int H, W; int dfs(int x, int y, int c){ if(col[y][x] == c) return 0; col[y][x] = c; int sum = 1; REP(i,4){ int tx= x+dx[i], ty=y+dy[i]; if(tx<0||W<=tx||ty<0||H<=ty||use[ty][tx]) continue; use[ty][tx] = true; sum += dfs(tx,ty,c); } return sum; } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); cin >> H >> W; REP(y,H) REP(x,W){ cin >> col[y][x]; } int Q; cin >> Q; bool fin = false; int c; while(Q--){ int y, x; cin >> y >> x >> c; --y,--x; if(!fin && col[y][x] != c){ fill((bool*)use, (bool*)use+200*200, false); use[y][x] = true; fin = dfs(x,y,c) == W*H; } } if(fin){ REP(y,H){ REP(x,W) cout << (x==0?"":" ") << c; cout << endl; } } else REP(y,H){ REP(x,W) cout << (x==0?"":" ") << col[y][x]; cout << endl; } return 0; }