#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;
	}
}