
C++#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n, m, P, First = 0x3f3f3f3f, tmp, dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int vis[21][21], dis[21][21];
struct node {
int x, y, step;
};
void bfs(int sx, int sy, int tx, int ty) {
queue<node> q;
node a;
q.push({sx, sy, 0});
while (!q.empty()) {
a = q.front(); q.pop();
if (a.step > First) return ;
if (a.x == tx && a.y == ty) {
if (dis[a.x][a.y] == 1) First = a.step;
}
else for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (abs(a.x - i) + abs(a.y - j) <= P && !vis[i][j] && !(a.x == i && a.y == j) && a.step + 1 <= First) {
q.push({i, j, a.step + 1});
dis[i][j]++;
}
}
}
}
}
int main() {
freopen("blessing.in", "r", stdin);
freopen("blessing.out", "w", stdout);
cin >> n >> m >> P;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> vis[i][j];
}
}
bfs(n, m, 1, 1);
cout << First << ' ' << dis[1][1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << dis[i][j] << ' ';
}
cout << '\n';
}
return 0;
}