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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    const int L = 0, R = 1, U = 2, D = 3;
    const int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

    for (;;) {
        int n, m; cin >> n >> m;
        if (!n) break;
        
        swap(n, m);

        vector<vector<vi>> mp(n, vector<vi>(m, vi(4, 1)));

        rep(i, 0, n) rep(j, 0, m) {
            if (i == 0) mp[i][j][U] = 0;
            if (i == n - 1) mp[i][j][D] = 0;
            if (j == 0) mp[i][j][L] = 0;
            if (j == m - 1) mp[i][j][R] = 0;
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m - 1; j++) {
                int t; cin >> t;
                // cout << "1#" << t << " ";
                mp[i][j][R] = mp[i][j + 1][L] = !t;
            }
            // cout << endl;
                
            if (i + 1 != n) {
                for (int j = 0; j < m; j++) {
                    int t; cin >> t;
                    // cout << "2#" << t << " ";
                    mp[i][j][D] = mp[i + 1][j][U] = !t;
                }
                // cout << endl;
            }
        }

        queue<pii> q;
        vector<vi> dist(n, vi(m, 0x3f3f3f3f));
        q.push({0, 0});
        dist[0][0] = 0;

        while (!q.empty()) {
            int x, y;
            tie(x, y) = q.front(); q.pop();
            for (int d = 0; d < 4; d++) {
                int nx = x + dir[d][0], ny = y + dir[d][1];
                if (mp[x][y][d] && dist[x][y] + 1 < dist[nx][ny]) {
                    dist[nx][ny] = dist[x][y] + 1;
                    q.push({nx, ny});
                }
            }
        }

        // rep(i, 0, n) {
        //     rep(j, 0, m) {
        //         cout << fixed << setw(10) << dist[i][j] << " ";
        //     }
        //     cout << endl;
        // }
        
        cout << (dist[n - 1][m - 1] == 0x3f3f3f3f ? 0 : dist[n - 1][m - 1] + 1) << endl;
    }
}



Measured memory_bytes: 3116