#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <cstdio>
#include <climits>
#include <complex>
#include <cstdint>
#include <tuple>

#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) {}
};

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<Edge>> AdjList;
AdjList graph;

const int INF = 100000000;



map<int, char> mp;

int t[10010];
int dxy[5]={-1, 0, 1, 0, -1};
int main() {
    //cout << fixed << setprecision(15);

    int w, h;
    while(cin >> w >> h){
        if(w == 0 && h == 0) break;

        vector<vector<int>> v(2*h-1);
        for(int i=0; i<2*h-1; ++i){
            if(i%2==0){
                for(int j=0; j<w-1; ++j){
                    int c = readInt();
                    v[i].push_back(c);
                }
            }else{
                for(int j=0; j<w; ++j){
                    int c = readInt();
                    v[i].push_back(c);
                }
            }
        }
        int d[50][50];
        REP(i, 50) REP(j, 50) d[i][j]=INT_MAX;
        d[0][0]=1;
        queue<pair<int, int>> q;
        q.push({0, 0});
        int gy = h-1, gx = w-1;

        while(q.size()){
            pair<int, int> p = q.front();
            q.pop();

            int y = p.first, x = p.second;

            if(y == gy && x == gx) break;

                if(y-1 >= 0 && y-1<h && d[y-1][x] == INT_MAX && v[y*2-1][x] == 0) {
                    d[y - 1][x] = d[y][x] + 1;
                    q.push({y - 1, x});
                }
                if(y + 1 < h && y+1>= 0 && d[y+1][x] == INT_MAX && v[y*2+1][x] == 0){
                    d[y+1][x] = d[y][x] + 1;
                    q.push({y+1, x});
                }
                if(x + 1 < w && x+1 >= 0 && d[y][x+1] == INT_MAX && v[2*y][x] == 0){
                    d[y][x+1] = d[y][x] + 1;
                    q.push({y, x+1});
                }
                if(x-1 <w && x-1 >= 0 && d[y][x-1] == INT_MAX && v[2*y][x-1] == 0){
                    d[y][x-1] = d[y][x] +1 ;
                    q.push({y, x-1});
                }

        }

        if(d[gy][gx] == INT_MAX){
            cout << 0 << endl;
        }else cout << d[gy][gx] << endl;
    }
    return 0;
}


Measured memory_bytes: 3256