#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) for (int i=0;i<(n);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define inf INT_MAX/3 #define INF INT_MAX/3 #define PB push_back #define MP make_pair #define ALL(a) (a).begin(),(a).end() #define SET(a,c) memset(a,c,sizeof a) #define CLR(a) memset(a,0,sizeof a) #define pii pair #define pcc pair #define pic pair #define pci pair #define VS vector #define VI vector #define DEBUG(x) cout<<#x<<": "<b?b:a) #define MAX(a,b) (a>b?a:b) #define pi 2*acos(0.0) #define INFILE() freopen("in0.txt","r",stdin) #define OUTFILE()freopen("out0.txt","w",stdout) #define in scanf #define out printf #define ll long long #define ull unsigned long long #define eps 1e-14 #define FST first #define SEC second int dx[] = { 1, 0, -1, 0 }; int dy[] = { 0, 1, 0, -1 }; queue > sel; int A[204][204]; void select(int i, int j, int x) { sel.push(MP(i, j)); for (int c = 0; c < 4; ++c) { if (A[i + dy[c]][j + dx[c]] == x) { A[i + dy[c]][j + dx[c]] = -2; select(i + dy[c], j + dx[c], x); } } } int main(void) { int H, W; memset(A, -1, sizeof(A)); cin >> H >> W; for (int h = 1; h <= H; ++h) { for (int w = 1; w <= W; ++w) { cin >> A[h][w]; } } int Q; cin >> Q; bool isEnd = false; int R, C, X; for (int count = 0; count < Q; ++count) { cin >> R >> C >> X; select(R, C, A[R][C]); if (sel.size() >= H*W) { isEnd = true; ++count; for (; count < Q; ++count) { cin >> R >> C >> X; } } while (!sel.empty()) { auto a = sel.front(); sel.pop(); A[a.first][a.second] = X; } } for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { if (isEnd) cout << X; else cout << A[i][j]; if (j != W) cout << " "; } cout << endl; } return 0; }