#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

//#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        int w,h; cin >> w >> h;
        dump(w)
        dump(h)
        if(w == 0 && h == 0) break;

        int wall[100][40] = {};

        REP(i,2*h-1){
            if(i % 2 == 0){
                REP(j,w-1){
                    cin >> wall[i][j];
                }
            }
            else{
                REP(j,w) cin >> wall[i][j];
            }
        }
        REP(i,2*h-1){
            REP(j,w) dump(wall[i][j])
        }
        queue<pair<PII,int>> que;
        int visited[40][40] = {};
        que.push(make_pair(PII(0,0),1));
        visited[0][0] = 1;
        bool flag = 1;
        while(!que.empty()){
            auto p = que.front(); que.pop();
            int nx = p.first.first;
            int ny = p.first.second;
            dump(nx)
            dump(ny)
            if(nx == h-1 && ny == w-1){
                cout << p.second << endl;
                flag = 0;
                break;
            }

            int dx[] = {-1,0,0,1};
            int dy[] = {0,-1,1,0};
            REP(i,4){
                int x = nx + dx[i];
                int y = ny + dy[i];
                if(0 <= x && x < h && 0 <= y && y < w && visited[x][y]==0){
                    if(i == 0){ //上
                        if(wall[nx*2-1][ny] == 0) {
                            que.push(make_pair(PII(x,y),p.second+1));
                            visited[x][y] = 1;
                        }
                    }
                    else if(i == 1){ //左
                        if(wall[nx*2][ny-1] == 0) {
                            que.push(make_pair(PII(x,y),p.second+1));
                            visited[x][y] = 1;

                        }
                    }
                    else if(i == 2){ //右
                        if(wall[nx*2][ny] == 0) {
                            que.push(make_pair(PII(x,y),p.second+1));
                            visited[x][y] = 1;

                        }
                    }
                    else{ //下
                        if(wall[nx*2+1][ny] == 0) {
                            que.push(make_pair(PII(x,y),p.second+1));
                            visited[x][y] = 1;
                        }
                    }
                }
            }

        }

        if(flag){
            cout << 0 << endl;
        }
    }

}


Measured memory_bytes: 3192