#include <queue>
#include <iostream>
#include <fstream>
#include <algorithm>
#define rep(i, n) for(int i=0; i<(n); ++i)

using namespace std;

//--global--//

struct wall{
    bool t, b, r, l;
};

//----------//

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//

    while(1){
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0)break;
        wall wall[35][35];
        bool res[35][35];
        rep(i, 35){
            fill(res[i], res[i]+35, false);
        }

        rep(i, h){
            rep(j, w){
                wall[i][j].t = false;
                wall[i][j].b = false;
                wall[i][j].l = false;
                wall[i][j].r = false;
                if(i == 0 ){
                    wall[i][j].t = true;
                }
                if(i == h-1){
                    wall[i][j].b = true;
                }
                if(j == 0){
                    wall[i][j].l = true;
                }
                if(j == w-1){
                    wall[i][j].r = true;
                }
            }
        }

        rep(i, h*2-1){
            if(i % 2== 0){
                rep(j, w-1){
                    int a;
                    cin >> a;
                    if(a == 1){
                        wall[i/2][j].r = true;
                        wall[i/2][j+1].l = true;
                    }
                }
            }
            else{
                rep(j, w){
                    int a;
                    cin >> a;
                    if(a == 1){
                        wall[i/2][j].b = true;
                        wall[i/2+1][j].t = true;
                    }
                }
            }
        }

        //cout << wall[0][0].t << endl;

        queue<pair<int, int> > q2;
        q2.push(make_pair(0, 0));
        int cnt = 0;
        bool ok = false;
        res[0][0] = true;
        while(1){

            queue<pair<int, int> > q;
            q = q2;
            while(!q2.empty()){
                q2.pop();
            }
            while(!q.empty()){
                pair<int, int> f = q.front(); q.pop();
                // t b 
                int x = f.first;
                int y = f.second;
                //cout << x << ", " << y << endl;
                if(x == w-1 && y == h-1){
                    ok = true;
                    break;
                }
                if(!wall[y][x].t) {
                    if(!res[y-1][x]){
                        q2.push(make_pair(x, y-1));
                        res[y-1][x] = true;
                    }
                }
                if(!wall[y][x].b) {
                    if(!res[y+1][x]){
                        q2.push(make_pair(x, y+1));
                        res[y+1][x] = true;
                    }
                }
                if(!wall[y][x].l) {
                    if(!res[y][x-1]){
                        q2.push(make_pair(x-1, y));
                        res[y][x-1] = true;
                    }
                }
                if(!wall[y][x].r) {
                    if(!res[y][x+1]){
                        q2.push(make_pair(x+1, y));
                        res[y][x+1] = true;
                    }
                }
            }
            cnt++;
            if(ok)break;
            if(q2.empty()){
                cout << 0 << endl;
                ok = false;
                break;
            }
        }

        if(ok)
            cout << cnt << endl;

    }
    
    return 0;
}



Measured memory_bytes: 3132