#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
typedef pair<int,int> P;
#define INF 1e9

int main(){
   ofstream outputfile("test.txt");
    int h,w;
	while(cin>>w>>h){
        if(w==0)break;
        //int k[2*h-1][w];
        int kx[h][w-1];
        int ky[h-1][w];
        int c=0,cc=0;
        memset(kx,0,sizeof(kx));
        memset(ky,0,sizeof(ky));
        for(int i=0;i<2*h-1;i++){
            if(i%2==0){
                for(int j=0;j<w-1;j++){
                    cin>>kx[c][j];
                }
                c++;
            }else{
                for(int j=0;j<w;j++){
                    cin>>ky[cc][j];
               }
                cc++;
            }
        }
        // for(int i=0;i<cc;i++){
        //     for(int j=0;j<w;j++)cout<<"ky:"<<ky[i][j]<<endl;
        // }
        int d[31][31];
        for(int i=0;i<31;i++)for(int j=0;j<31;j++)d[i][j]=INF;
        int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
        int sx=0,sy=0,gx=w-1,gy=h-1;
       // int m[h][w];memset(m,0,sizeof(0));
        queue<P> que;
        que.push(P(sy,sx));
        d[sy][sx]=0;
        while(que.size()){
            P p = que.front(); que.pop();
            if(p.first==gy&&p.second==gx)break;
            for(int i=0;i<4;i++){
                int ny = p.first + dy[i], nx = p.second + dx[i];
                //if(nx<0||ny<0)continue;
              //  cout<<"nx:"<<nx<<" ny:"<<ny<<" kx:"<<k[(ny+1)*2][nx-1]<<" ky:"<<k[(ny+1)*2-1][nx]<<endl;
                //if(k[(ny+1)*2][nx-1]==1)nx-=dx[i];
                //if(k[(ny+1)*2-1][nx]==1)ny-=dy[i];
                int tmp=0;
                if(dx[i]==-1||dy[i]==-1){
                    if(dx[i])tmp = 0;
                    else tmp = 0;
                }else{
                    if(dx[i])tmp = dx[i];
                    else tmp = dy[i];
                }
                if(dx[i]!=0&&kx[ny][nx-tmp]==1){
                     //cout<<"dn:"<<nx<<" "<<ny<<endl;
                    continue;
                }
                if(dy[i]!=0&&ky[ny-tmp][nx]==1){
                    // cout<<"nn:"<<nx<<" "<<ny-dy[i]<<endl;
                    // cout<<"kn:"<<ky[nx][ny-dy[i]]<<endl;
                     //cout<<"dny:"<<nx<<" "<<ny<<endl;
                    continue;
                }
                if(nx>=0&&nx<w&&0<=ny&&ny<h&&d[ny][nx]==INF){
                   //壁の判定
                  // cout<<"nx:"<<nx<<" ny:"<<ny<<" kx:"<<k[ny*2][nx-1]<<" ky:"<<k[(ny+1)*2-1][nx]<<endl;
                 // cout<<"nx:"<<nx<<" ny:"<<ny<<endl;
                   // if(k[ny*2][nx-1]!=1||k[ny*2-1][nx]!=1){
                     //   cout<<"n:"<<nx<<" "<<ny<<endl;
                     //cout<<"n:"<<nx<<" "<<ny<<" dx,dy:"<<dx[i]<<" "<<dy[i]<<" kx,ky:"<<kx[ny][nx-tmp]<<" ky:"<<ky[ny-tmp][nx]<<" d:"<<d[p.first][p.second] + 1<<endl;
                        que.push(P(ny,nx));
                        d[ny][nx] = d[p.first][p.second] + 1;
                   // }
                }
            }
        }

        // for(int i=0;i<h;i++){
        //     for(int j=0;j<w;j++){
        //         cout<<d[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }

        if(d[gy][gx]==INF)d[gy][gx]=0;
        else d[gy][gx]++;
        cout<<d[gy][gx]<<endl;
        outputfile<<d[gy][gx]<<endl;
    }
     outputfile.close();
}


Measured memory_bytes: 3076