#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 1<<30
#define MOD 1000000007
#define mod 998244353
#define REP(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp(pair<ll,ll>a,pair<ll,ll> b){
        if(a.se!=b.se)
        return a.se<b.se;
        else
        return a.fi<b.fi;
}
//----------------------------------------------------------------------
int h,w;
bool color[40][40];
bool cannot[40][40][5];
int d[40][40];
//----------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_time=clock();
    //-------------------------------
    cin>>w>>h;
    while(w&&h){
        memset(color,0,sizeof(color));
        memset(cannot,0,sizeof(cannot));
        for(int i=0;i<40;i++){
            for(int j=0;j<40;j++){
                d[i][j]=IINF;
            }
        }
        d[0][0]=1;
        for(int i=0;i<2*h-1;i++){
            int len;
            if(i%2==0)len=w-1;
            else len=w;
            //cout<<i<<" "<<len<<endl;
            for(int j=0;j<len;j++){
                int a;cin>>a;
                //cout<<i<<" "<<j<<" "<<a<<endl;
                if(a==1){
                    if(i%2==0){
                        //cout<<i/2<<" "<<j<<endl;
                        cannot[i/2][j][2]=1;
                        cannot[i/2][j+1][1]=1;
                    }
                    else{
                        //cout<<i/2<<" "<<j<<endl;
                        cannot[i/2][j][4]=1;
                        cannot[i/2+1][j][3]=1;
                    }
                }
            }
        }
        queue<Pin>q;
        q.push(mp(0,0));
        color[0][0]=1;
        while(!q.empty()){
            Pin tmp=q.front();q.pop();
            int x=tmp.fi;int y=tmp.se;
            //cout<<x<<" "<<y<<endl;
            int nx,ny;
            if(x>0){
                if(cannot[x][y][3]==0){
                    nx=x-1,ny=y;
                    if(color[nx][ny]==0){
                    color[nx][ny]=1;
                    q.push(mp(nx,ny));
                    chmin(d[nx][ny],d[x][y]+1);}
                }
            }
            if(x<h-1){
                if(cannot[x][y][4]==0){
                    nx=x+1,ny=y;
                    if(color[nx][ny]==0){
                    color[nx][ny]=1;
                    q.push(mp(nx,ny));
                    chmin(d[nx][ny],d[x][y]+1);}
                }
            }
            if(y>0){
                if(cannot[x][y][1]==0){
                    nx=x,ny=y-1;
                    if(color[nx][ny]==0){
                    color[nx][ny]=1;
                    q.push(mp(nx,ny));
                    chmin(d[nx][ny],d[x][y]+1);}
                }
            }
            if(y<w-1){
                if(cannot[x][y][2]==0){
                    nx=x,ny=y+1;
                    if(color[nx][ny]==0){
                    color[nx][ny]=1;
                    q.push(mp(nx,ny));
                    chmin(d[nx][ny],d[x][y]+1);}
                }
            }
        }
        
        if(d[h-1][w-1]==IINF)d[h-1][w-1]=0;
        cout<<d[h-1][w-1]<<endl;
        cin>>w>>h;
    }
    //-------------------------------  
    //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------





























Measured memory_bytes: 3136