include[HTML_REMOVED]
using namespace std;
int q[1010][1010],p[1010][1010],n,m;
int a[5]={0,1,-1,0,0};
int b[5]={0,0,0,1,-1};
void dfs(int temp,int x,int y){
p[x][y]=temp;
for(int i=1;i<=4;i){
int u=a[i]+x;
int v=b[i]+y;
if(!q[u][v]&&temp+1[HTML_REMOVED]>n>>m;
for(int i=1;i<=n;i)for(int j=1;j<=m;j)p[i][j]=INT_MAX;
for(int i=1;i<=n;i)for(int j=1;j<=m;j++)cin>>q[i][j];
dfs(0,1,1);
cout<<p[n][m];
}
//dfs简简单单,不用return;