#include #include #include using namespace std; int main() { int N, M; cin >> N >> M; vector> A(N, vector(M)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> A[i][j]; } } vector> graph(N*M); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int x = i * M + j; if (i > 0) { graph[x].push_back(x - M); } if (j > 0) { graph[x].push_back(x - 1); } if (i < N - 1) { graph[x].push_back(x + M); } if (j < M - 1) { graph[x].push_back(x + 1); } } } vector colors(N*M); vector length(N*M); queue q; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (A[i][j] == 1) { int x = i * M + j; q.push(x); colors[x] = 1; } } } while (!q.empty()) { int u = q.front(); q.pop(); for (int v : graph[u]) { if (colors[v] == 0) { colors[v] = 1; length[v] = length[u] + 1; q.push(v); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int x = i * M + j; cout << length[x] << " "; } cout << "\n"; } return 0; }