#include<iostream>
#include<queue>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
vector<vector<int> > a, used, dist;
vector<vector<pair<int,int> > > par;
int n,m;
void bfs(){
queue<pair<int,int> > q;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++){
if(a[i][j]==1){
q.push({i,j});
used[i][j]=1;
}
}
while(!q.empty()){
pair<int,int> u=q.front();
q.pop();
int x=u.first, y=u.second;
int dx[4]={-1,0,1, 0};
int dy[4]={ 0,1,0,-1};
for(int i=0;i<4;i++){
int l=x+dx[i], r=y+dy[i];
if(l>=0 and l<=n-1 and r>=0 and r<=m-1 and used[l][r]==0){
used[l][r]=1;
dist[l][r]=dist[x][y]+1;
par[l][r]={x,y};
q.push({l,r});
}
}
}
}
int main(){
cin>>n>>m;
a.resize(n,vector<int>(m,0));
used.resize(n,vector<int>(m,0));
dist.resize(n,vector<int>(m,0));
par.resize(n,vector<pair<int,int> >(m,{-1,-1}));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
cin>>a[i][j];
bfs();
for(int i=0; i<n; i++){
for(int j=0; j<m; j++)
cout<<dist[i][j]<<" ";
cout<<endl;
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTx2ZWN0b3I+CiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3Rvcjx2ZWN0b3I8aW50PiA+IGEsIHVzZWQsIGRpc3Q7CnZlY3Rvcjx2ZWN0b3I8cGFpcjxpbnQsaW50PiA+ID4gcGFyOwppbnQgbixtOwp2b2lkIGJmcygpewoJcXVldWU8cGFpcjxpbnQsaW50PiA+IHE7Cglmb3IoaW50IGk9MDsgaTxuOyBpKyspCgkJZm9yKGludCBqPTA7IGo8bTsgaisrKXsKCQkJaWYoYVtpXVtqXT09MSl7CgkJCQlxLnB1c2goe2ksan0pOwoJCQkJdXNlZFtpXVtqXT0xOwoJCQl9CgkJfQoJd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