#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<queue>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll,ll> LLP;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
#define INF 1000000007
#define MOD 1000000007
#define ARRAY_MAX 70

char mp[ARRAY_MAX][ARRAY_MAX];
int sx,sy;//スタート
int gx,gy;//ゴール
int h,w;
int d[ARRAY_MAX][ARRAY_MAX];

int bfs(){

    queue<P> que;
    REP(i,ARRAY_MAX){
        REP(j,ARRAY_MAX){
            d[i][j] = INF;//最短距離の初期化
        }
    }

    que.push(P(sy,sx));//y,xの順
    d[sy][sx] = 0;

    while(que.size()){

        P p = que.front();//first:y,second:x
        que.pop();
        if(p.first == gy && p.second == gx){
            break;
        }
        for(int i = 0;i < 4;i++){
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];

            if(0 <= nx && nx < 2*w-1 && 0 <= ny && ny < 2*h-1 && mp[ny][nx] != '*' && d[ny][nx] == INF){
                //移動先が配列内かつ道である場合
                que.push(P(ny,nx));

                if((i == 0 && nx % 2 == 1) || (i == 1 && ny % 2 == 1) || (i == 2 && ny % 2 == 1) || (i == 3 && nx % 2 == 1)){
                    d[ny][nx] = d[p.first][p.second];
                }else{
                    d[ny][nx] = d[p.first][p.second] + 1;
                }
            }
        }
    }
    return d[gy][gx];
}



int main(){

    while(cin >> w >> h,w){
        
        sx = sy = 0;
        gx = 2*w-2;
        gy = 2*h-2;
        REP(i,ARRAY_MAX){
            REP(j,ARRAY_MAX){
                //とりあえず全て*で埋める
                //道は後で置き換える
                mp[i][j] = '*';
            }
        }

        REP(i,2*h-1){

            if(i % 2 == 0){
                //縦棒を構築
                for(int j = 0;j < 2*w;j+=2){
                    mp[i][j] = '&';//道に直す
                }
                REP(j,w-1){
                    int a;
                    cin >> a;
                    if(!a){
                        //壁がないなら道に直す
                        mp[i][2*j+1] = '&';
                    }else{
                        mp[i][2*j+1] = '*';
                    }
                }
            }else{
                //横棒を構築
                REP(j,w){
                    int a;
                    cin >> a;
                    if(!a){
                        //壁でないなら道に直す
                        mp[i][2*j] = '&';
                    }else{
                        mp[i][2*j] = '*';
                    }
                }
            }
        }

        //迷路の構築終了
        int ans = bfs();
        if(ans == INF){
            cout << "0" << endl;
        }else{
            cout << ans+1 << endl;
        }
        /*REP(i,2*h-1){
            REP(j,2*w-1){
                cout << d[i][j] << "    ";
            }
            cout << endl;
        }
        cout << endl;*/
    }

    return 0;
}



Measured memory_bytes: 3112