#include<bits/stdc++.h>
using namespace std;
typedef long long L;
L w,h,grid[31][31][5],dist[31][31],flg=0;
L X[]={0,1,0,-1};
L Y[]={1,0,-1,0};
void reset(){
    memset(grid,0,sizeof(grid));
    memset(dist,0,sizeof(dist));
    flg=0;
}
void dfs(L x,L y){
    if(x==h&&y==w){
        flg=1;
        return ;
    }
    for(L i=0;i<4;i++){
        L xx=X[i]+x,yy=Y[i]+y;
        if(xx==1&&yy==1)continue;
        if(xx>0&&xx<=h && yy>0&&yy<=w){
            if(i==0){
                if(grid[x][y][0]){
                    if(dist[xx][yy]==0){
                        dist[xx][yy]=dist[x][y]+1;
                        //cout<<"in >> "<<xx<<" "<<yy<<endl;
                        dfs(xx,yy);
                        //cout<<"out >> "<<xx<<" "<<yy<<endl;
                    }
                    else{
                        if(dist[xx][yy]>dist[x][y]+1){
                            dist[xx][yy]=dist[x][y]+1;
                            //cout<<"in >> "<<xx<<" "<<yy<<endl;
                            dfs(xx,yy);
                            //cout<<"out >> "<<xx<<" "<<yy<<endl;
                        }
                    }
                }
            }
            else if(i==1){
                if(grid[x][y][1]){
                    if(dist[xx][yy]==0){
                        dist[xx][yy]=dist[x][y]+1;
                        //cout<<"in >> "<<xx<<" "<<yy<<endl;
                        dfs(xx,yy);
                        //cout<<"out >> "<<xx<<" "<<yy<<endl;
                    }
                    else{
                        if(dist[xx][yy]>dist[x][y]+1){
                            dist[xx][yy]=dist[x][y]+1;
                            //cout<<"in >> "<<xx<<" "<<yy<<endl;
                            dfs(xx,yy);
                            //cout<<"out >> "<<xx<<" "<<yy<<endl;
                        }
                    }
                }
            }
            else if(i==2){
                if(grid[x][y][2]){
                    if(dist[xx][yy]==0){
                        dist[xx][yy]=dist[x][y]+1;
                        //cout<<"in >> "<<xx<<" "<<yy<<endl;
                        dfs(xx,yy);
                        //cout<<"out >> "<<xx<<" "<<yy<<endl;
                    }
                    else{
                        if(dist[xx][yy]>dist[x][y]+1){
                            dist[xx][yy]=dist[x][y]+1;
                            //cout<<"in >> "<<xx<<" "<<yy<<endl;
                            dfs(xx,yy);
                            //cout<<"out >> "<<xx<<" "<<yy<<endl;
                        }
                    }
                }
            }
            else if(i==3){
                if(grid[x][y][3]){
                    if(dist[xx][yy]==0){
                        dist[xx][yy]=dist[x][y]+1;
                        //cout<<"in >> "<<xx<<" "<<yy<<endl;
                        dfs(xx,yy);
                        //cout<<"out >> "<<xx<<" "<<yy<<endl;
                    }
                    else{
                        if(dist[xx][yy]>dist[x][y]+1){
                            dist[xx][yy]=dist[x][y]+1;
                            //cout<<"in >> "<<xx<<" "<<yy<<endl;
                            dfs(xx,yy);
                            //cout<<"out >> "<<xx<<" "<<yy<<endl;
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    while(scanf("%lld%lld",&w,&h)&& w||h ){
        L r=h,c=w,b;
        for(L i=1;i<=r;i++){
            if(i!=r){
                for(L j=1;j<c;j++){
                    scanf("%lld",&b);
                    if(!b){
                        grid[i][j][0]=1;grid[i][j+1][2]=1;
                    }
                }
                for(L j=1;j<=c;j++){
                    scanf("%lld",&b);
                    if(!b){
                        grid[i][j][1]=1;grid[i+1][j][3]=1;
                    }
                }
            }
            else{
                for(L j=1;j<c;j++){
                    scanf("%lld",&b);
                    if(!b){
                        grid[i][j][0]=1;grid[i][j+1][2]=1;
                    }
                }
            }
        }
        dfs(1,1);
        if(flg)printf("%lld\n",(dist[h][w])+1);
        else printf("0\n");
        reset();
    }
    return 0;
}
/*
6 5
0 1 0 0 1
0 0 1 0 1 1
0 1 1 0 0
0 1 0 0 1 0
1 1 0 1 0
0 1 1 0 0 1
0 1 1 1 0
0 1 1 0 0 1
0 0 0 1 0

*/



Measured memory_bytes: 3288