p1256
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1005, mod = 0, Maxn = 1e9;
const int fx[10] = {0, -1, 0, 1, 0};
const int fy[10] = {0, 0, 1, 0, -1};
int dp[N][N], n, m, a[N][N], posx, posy;
inline int read()
{
int x = 0,f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if(ch == '-')
f = -1; ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - 48; ch = getchar();
}
return x * f;
}
int dfs(int x, int y)
{
if(x < 1 || x > n || y < 1 || y > m)
return dp[x][y] = Maxn;
if(dp[x][y] != Maxn)
return dp[x][y];
if(a[x][y] == 1)
dp[x][y] = 0;
for(int i = 1; i <= 4; i++)
{
int nx = fx[i] + x;
int ny = fy[i] + y;
dp[x][y] = min(dp[x][y], dfs(nx, ny) + 1);
}
return dp[x][y];
}
signed main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
for(int j = 0; j < s.size(); j++)
{
a[i][j + 1] = (int)(s[j] - '0'), dp[i][j + 1] = Maxn;
if(a[i][j + 1] == 1)
posx = i, posy = j + 1;
}
}
int f = dfs(posx, posy);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
cout << dp[i][j] << " ";
cout << endl;
}
return 0;
}