#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;

vi move1 = {0,0,1,-1};
vi move2 = {1,-1,0,0};
/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);

    vec<int> ans;

    int h,w;
    bool cantGO;
    vec<pair<PII,PII>> WallInfo_init;
    vec<pair<PII,PII>> WallInfo;
    queue<PII> que;

    while(cin >> w >> h) {
        if(h==0&&w==0) break;
        
        WallInfo = WallInfo_init;
        vii dist(h, vi(w));

        // 各壁を各部屋からどの部屋に行くかで定義する.
        for(int i = 0; i < 2*h-1; ++i) {
            if(i%2==0) rep(j,w-1) {
                cin >> cantGO;
                if(cantGO) {
                    WallInfo.emplace_back(make_pair(i/2,j), make_pair(i/2,j+1));
                    WallInfo.emplace_back(make_pair(i/2,j+1), make_pair(i/2,j));
                }
            }
            else rep(j,w) {
                cin >> cantGO;
                if(cantGO) {
                    WallInfo.emplace_back(make_pair(i/2,j), make_pair(i/2+1,j));
                    WallInfo.emplace_back(make_pair(i/2+1,j), make_pair(i/2,j));
                }
            }
        }
        sort(all(WallInfo));

        que.emplace(0,0);
        dist[0][0] = 1;
        while(que.size()) {
            int x = que.front().first;
            int y = que.front().second;
            que.pop();

            rep(i,4) {
                int nx = x + move1[i];
                int ny = y + move2[i];
                if(!(0<=nx && nx<h && 0<=ny && ny<w)) continue;
                if(dist[nx][ny] != 0) continue;
                pair<PII,PII> pp = make_pair(make_pair(x,y), make_pair(nx,ny));
                if(pp ==  *lower_bound(all(WallInfo), pp)) continue;
                dist[nx][ny] = dist[x][y] + 1;
                que.emplace(nx,ny);
            }
        }
        // rep(i,h)rep(j,w) cout << dist[i][j] << (j==w-1 ? '\n' : ' ');

        ans.push_back(dist[h-1][w-1]);
        
    }

    for(auto answer : ans) cout << answer << '\n';

    // printf("ExcutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}


Measured memory_bytes: 3184