#include <iostream>
#include<string>
#include<queue>
using namespace std;
int map[5][5]={
{0,1,0,0,0},
{0,0,0,1,0},
{1,0,1,1,0},
{1,0,0,1,0},
{1,1,0,0,-1}
};
int v[5][5]={0};
struct node{
int x,y,step;
node(int xx,int yy,int ss)
{
x =xx;
y=yy;
step = ss;
}
};
bool in_map(int x,int y);
int bfs(int x,int y);
int main()
{
cout<<bfs(0,0)<<endl;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<v[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
bool in_map(int x,int y)
{
return x>=0&&x<=4&&y>=0&&y<=4;
}
int bfs(int x,int y)
{
v[x][y] = 1;
node nod(x,y,0);
queue<node> qu;
qu.push(nod);
while(!qu.empty())
{
nod = qu.front();
qu.pop();
x = nod.x;
y = nod.y;
if(in_map(x+1,y)&&v[x+1][y]==0&&map[x+1][y]!=1)
{
v[x+1][y]=1;
if(map[x+1][y] == -1)
{
return nod.step+1;
}
else
{
qu.push(node(x+1,y,nod.step+1));
}
}
if(in_map(x,y+1)&&v[x][y+1]==0&&map[x][y+1]!=1)
{
v[x][y+1]=1;
if(map[x][y+1] == -1)
{
return nod.step+1;
}
else
{
qu.push(node(x,y+1,nod.step+1));
}
}
if(in_map(x-1,y)&&v[x-1][y]==0&&map[x-1][y]!=1)
{
v[x-1][y] = 1;
if(map[x-1][y] == -1)
{
return nod.step+1;
}
else
{
qu.push(node(x-1,y,nod.step+1));
}
}
if(in_map(x,y-1)&&v[x][y-1]==0&&map[x][y-1]!=1)
{
v[x][y-1] = 1;
if(map[x][y-1] == -1)
{
return nod.step+1;
}
else
{
qu.push(node(x,y-1,nod.step+1));
}
}
}
return -1;
}
// int get_people(queue<int> qu,int m)
// {
// if(!qu.empty() && qu.size()<=m){
// return -1;
// }
// int acount=1;
// int t;
// while(!qu.empty())
// {
// t = qu.front();
// if(acount<m)
// {
// acount++;
// qu.pop();
// qu.push(t);
// }
// else
// {
// acount = 1;
// qu.pop();
// }
// }
// return t;
// }
// int main()
// {
// int n,m;
// cin>>n>>m;
// queue<int> qu;
// for(int i = 0;i<n;i++)
// {
// qu.push(i+1);
// }
// cout<<get_people(qu,m);
// return 0;
// }
// int map[5][5]={
// {0,1,1,1,1},
// {0,0,0,1,1},
// {1,0,1,1,1},
// {1,0,0,0,1},
// {1,1,1,0,-1}
// };
// int v[5][5]={0};
// bool dfs(int x,int y);
// bool in_map(int x,int y);
// int main() {
// cout<<dfs(0,0)<<endl;
// for(int i=0;i<5;i++)
// {
// for(int j=0;j<5;j++)
// {
// cout<<v[i][j]<<", ";
// }
// cout<<endl;
// }
// return 0;
// }
// bool in_map(int x,int y)
// {
// return x>=0&&x<=4&&y>=0&&y<=4;
// }
// bool dfs(int x,int y)
// {
// v[x][y]=1;
// if(map[x][y]==-1)
// {
// return true;
// }
// map[x][y] = 8;
// if(in_map(x-1,y)&&v[x-1][y]==0&&map[x-1][y]!=1)
// {
// if(dfs(x-1,y)==true)
// {
// return true;
// }
// }
// if(in_map(x,y-1)&&v[x][y-1]==0&&map[x][y-1]!=1)
// {
// if(dfs(x,y-1)==true)
// {
// return true;
// }
// }
// if(in_map(x+1,y)&&v[x+1][y]==0&&map[x+1][y]!=1)
// {
// if(dfs(x+1,y)==true)
// {
// return true;
// }
// }
// if(in_map(x,y+1)&&v[x][y+1]==0&&map[x][y+1]!=1)
// {
// if(dfs(x,y+1)==true)
// {
// return true;
// }
// }
// return false;
// }