#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int w, h;
struct sqdata {
    int left; int up; int right; int down;
};
struct xydata {
    int x; int y; int cnt;
};

int main() {
    while(1) {
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector< vector<sqdata> > maze(h, vector<sqdata>(w));
        vector< vector<bool> > visitmaze(h, vector<bool>(w, false));
        for(int i = 0; i < 2*h-1; ++i) {
            int wall;
            if(i % 2 == 0) {
                int imaze = i/2;
                for(int j = 0; j < w-1; ++j) {
                    cin >> wall;
                    if(j == 0) {
                        maze.at(imaze).at(j).left = 1;
                    }
                    maze.at(imaze).at(j).right = wall;
                    maze.at(imaze).at(j+1).left = wall;
                    if(j+1 == w-1) {
                        maze.at(imaze).at(j+1).right = 1;
                    }
                }
            }else {
                int imaze = i/2;
                for(int j = 0; j < w; ++j) {
                    cin >> wall;
                    if(imaze == 0) {
                        maze.at(imaze).at(j).up = 1;
                    }
                    maze.at(imaze).at(j).down = wall;
                    maze.at(imaze+1).at(j).up = wall;
                    if(imaze+1 == h-1) {
                        maze.at(imaze+1).at(j).down = 1;
                    }
                }
            }
        }

        /*for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                cout << "x y " << j << " " << i << ": ";
                cout << "left up right down ";
                cout << maze.at(i).at(j).left << " " << maze.at(i).at(j).up << " " << maze.at(i).at(j).right << " " << maze.at(i).at(j).down << endl;
            }
        }*/

        xydata first;
        first.x = 0; first.y = 0; first.cnt = 1;
        queue<xydata> que;
        que.push(first);

        int ans = 0;
        while(!que.empty()) {
            xydata now = que.front();
            que.pop();
            if(now.x == w-1 && now.y == h-1) {
                ans = now.cnt;
                break;
            }
            int nx, ny;
            int ncnt = now.cnt+1;
            if(maze.at(now.y).at(now.x).left == 0) {
                nx = now.x-1; ny = now.y;
                if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                    if(!visitmaze.at(ny).at(nx)) {
                        visitmaze.at(ny).at(nx) = true;
                        xydata next;
                        next.x = nx; next.y = ny; next.cnt = ncnt;
                        que.push(next);
                    }
                }
            }
            if(maze.at(now.y).at(now.x).up == 0) {
                nx = now.x; ny = now.y-1;
                if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                    if(!visitmaze.at(ny).at(nx)) {
                        visitmaze.at(ny).at(nx) = true;
                        xydata next;
                        next.x = nx; next.y = ny; next.cnt = ncnt;
                        que.push(next);
                    }
                }
            }
            if(maze.at(now.y).at(now.x).right == 0) {
                nx = now.x+1; ny = now.y;
                if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                    if(!visitmaze.at(ny).at(nx)) {
                        visitmaze.at(ny).at(nx) = true;
                        xydata next;
                        next.x = nx; next.y = ny; next.cnt = ncnt;
                        que.push(next);
                    }
                }
            }
            if(maze.at(now.y).at(now.x).down == 0) {
                nx = now.x; ny = now.y+1;
                if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                    if(!visitmaze.at(ny).at(nx)) {
                        visitmaze.at(ny).at(nx) = true;
                        xydata next;
                        next.x = nx; next.y = ny; next.cnt = ncnt;
                        que.push(next);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 3124