// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep4(i, a, b, c) for (int i = int(a), i##__b = int(b), i##__c = int(c); i < i##__b; i += i##__c)
#define rep1(n) rep4(i, 0, n, 1)
#define rep2(i, n) rep4(i, 0, n, 1)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define REP(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep3(i, a, b) for (int i = int(b) - 1, i##__a = int(a); i >= i##__a; i--)
#define rrep1(n) rrep3(i, 0, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep4(i, a, b, c) for (int i = int(a + (b - a - 1) / c * c), i##__a = int(a), i##__c = int(c); i >= i##__a; i -= i##__c)
#define RREP(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define FOREACH(i, x) for (auto &i : x)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define print(x) cout << x << endl;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vbool = vector<bool>;
using vvbool = vector<vector<bool>>;
using vstring = vector<string>;
template <class T> using vv = vector<vector<T>>;
template <class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return 1;}return 0;}
template <class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return 1;}return 0;}

struct Point {int x, y; Point(int x, int y):x(x), y(y){}};
int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1};
pii dvs[] = {mp(1, 0), mp(0, 1), mp(-1, 0), mp(0, -1)};
// clang-format on


int main() {
    int W, H;
    cin >> W >> H;
    for (; W != 0 || H != 0; cin >> W >> H) {
        // 座標 : 0-indexed
        // 座標(x,y)の左に壁があるとき : l[y][x]==1
        // 座標(x,y)の右に壁があるとき : l[y][x+1]==1
        vvint l(H, vint(W + 1, 1));
        vvint u(H + 1, vint(W, 1));

        REP(x, 1, W) {
            cin >> l[0][x];
        }
        REP(y, 1, H) {
            REP(x, W) {
                cin >> u[y][x];
            }
            REP(x, 1, W) {
                cin >> l[y][x];
            }
        }
        queue<pii> q;
        vvint saitan(H, vint(W, -1));
        saitan[0][0] = 1;
        int x = 0, y = 0;
        for (; !(x == W - 1 && y == H - 1); tie(x, y) = q.front(), q.pop()) {
            // 左の更新
            if (!l[y][x] && saitan[y][x - 1] == -1) {
                q.push(mp(x - 1, y));
                saitan[y][x - 1] = saitan[y][x] + 1;
            }
            // 右の更新
            if (!l[y][x + 1] && saitan[y][x + 1] == -1) {
                q.push(mp(x + 1, y));
                saitan[y][x + 1] = saitan[y][x] + 1;
            }
            // 上の更新
            if (!u[y][x] && saitan[y - 1][x] == -1) {
                q.push(mp(x, y - 1));
                saitan[y - 1][x] = saitan[y][x] + 1;
            }
            // 下の更新
            if (!u[y + 1][x] && saitan[y + 1][x] == -1) {
                q.push(mp(x, y + 1));
                saitan[y + 1][x] = saitan[y][x] + 1;
            }
            if (q.empty()) break;
        }

        if (saitan[H - 1][W - 1] == -1) {
            print(0);
        } else {
            print(saitan[H - 1][W - 1]);
        }
    }
}


Measured memory_bytes: 3144