#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 100;
bool visited[maxn][maxn];
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int m, n;
struct Node
{
int wall;
int step;
};
struct Pot
{
Pot(int x, int y):x(x),y(y){}
int x;
int y;
};
Node map[maxn][maxn];
void bfs(int x, int y)
{
int steps = 0;
queue<Pot> Q;
Pot start(x, y);
visited[x][y] = 1;
map[x][y].step = steps;
Q.push(start);
int i;
while(!Q.empty())
{
Pot now = Q.front();
Q.pop();
steps = map[now.x][now.y].step;
for(i=0;i<4;i++)
{
int nx = now.x+move[i][0];
int ny = now.y+move[i][1];
if(nx>=0 && nx<m && ny>=0 && ny<n && !visited[nx][ny] && !map[nx][ny].wall)
{
Pot p(nx, ny);
map[nx][ny].step = steps+1;
Q.push(p);
visited[nx][ny]=true;
}
}
}
}
int main()
{
memset(visited, false, sizeof(visited));
cin>>m>>n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>map[i][j].wall;
bfs(0,0);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<map[i][j].step<<" ";
cout<<endl;
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 100;
bool visited[maxn][maxn];
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int m, n;
struct Node
{
int wall;
int step;
};
struct Pot
{
Pot(int x, int y):x(x),y(y){}
int x;
int y;
};
Node map[maxn][maxn];
void bfs(int x, int y)
{
int steps = 0;
queue<Pot> Q;
Pot start(x, y);
visited[x][y] = 1;
map[x][y].step = steps;
Q.push(start);
int i;
while(!Q.empty())
{
Pot now = Q.front();
Q.pop();
steps = map[now.x][now.y].step;
for(i=0;i<4;i++)
{
int nx = now.x+move[i][0];
int ny = now.y+move[i][1];
if(nx>=0 && nx<m && ny>=0 && ny<n && !visited[nx][ny] && !map[nx][ny].wall)
{
Pot p(nx, ny);
map[nx][ny].step = steps+1;
Q.push(p);
visited[nx][ny]=true;
}
}
}
}
int main()
{
memset(visited, false, sizeof(visited));
cin>>m>>n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>map[i][j].wall;
bfs(0,0);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<map[i][j].step<<" ";
cout<<endl;
}
return 0;
}