#include using namespace std; const int MAXN = 107; const pair vec[8] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; int a[MAXN][MAXN], depth[MAXN][MAXN]; short used[MAXN][MAXN]; bool is_cycle[MAXN][MAXN]; int len_cycle[MAXN][MAXN]; int c, r; int ans[MAXN][MAXN]; vector > order; bool is_valid(int x, int y){ if(x >= 0 && x < r && y >= 0 && y < c) return true; return false; } void mark_cycle(int x, int y, int to_x, int to_y, int len){ int nxt_x = x + vec[a[x][y]].first; int nxt_y = y + vec[a[x][y]].second; is_cycle[x][y] = true; len_cycle[x][y] = len; if(x != to_x || y != to_y){ mark_cycle(nxt_x, nxt_y, to_x, to_y, len); } } void dfs(int x, int y, int d = 0){ used[x][y] = 2; depth[x][y] = d; int nxt_x = x + vec[a[x][y]].first; int nxt_y = y + vec[a[x][y]].second; if(!is_valid(nxt_x, nxt_y)){ used[x][y] = 1; return; } if(!used[nxt_x][nxt_y]){ dfs(nxt_x, nxt_y, d + 1); } else{ int len = d - depth[nxt_x][nxt_y] + 1; if(used[nxt_x][nxt_y] == 2) mark_cycle(nxt_x, nxt_y, x, y, len); } used[x][y] = 1; } void dfs_order(int x, int y){ int nxt_x = x + vec[a[x][y]].first; int nxt_y = y + vec[a[x][y]].second; used[x][y] = true; if(!is_valid(nxt_x, nxt_y)) return; if(!used[nxt_x][nxt_y]){ dfs_order(nxt_x, nxt_y); } order.push_back(make_pair(x, y)); } void dfs_sim(int x, int y, long long moves, bool ok = true, int cnt = 0){ if(used[x][y]){ if(ok && is_cycle[x][y]){ moves %= len_cycle[x][y]; ok = false; } } else ++cnt; used[x][y] = true; if(!moves){ ++ans[x][y]; --cnt; if(!cnt){ return; } ++moves; } int nxt_x = x + vec[a[x][y]].first; int nxt_y = y + vec[a[x][y]].second; if(!is_valid(nxt_x, nxt_y)) return; dfs_sim(nxt_x, nxt_y, moves - 1, ok, cnt); } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); freopen("flowfield.in", "r", stdin); freopen("flowfield.out", "w", stdout); long long s; cin >> c >> r >> s; for(int i = 0; i < r; ++i){ for(int j = 0; j < c; ++j){ cin >> a[i][j]; } } for(int i = 0; i < r; ++i){ for(int j = 0; j < c; ++j){ if(!used[i][j]) dfs_order(i, j); } } for(int i = 0; i < r; ++i) for(int j = 0; j < c; ++j) used[i][j] = 0; reverse(order.begin(), order.end()); for(pair p: order){ int x = p.first; int y = p.second; if(!used[x][y]){ dfs(x, y); } } for(int i = 0; i < r; ++i) for(int j = 0; j < c; ++j) used[i][j] = 0; for(pair p: order){ int x = p.first; int y = p.second; if(!used[x][y]){ dfs_sim(x, y, s); } } for(int i = 0; i < r; ++i){ for(int j = 0; j < c; ++j){ cout << ans[i][j] << " "; } cout << "\n"; } return 0; }