#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAX = 33;
const int INF = 1e9;
bool ok[MAX][MAX][4];
vector<vi> d[MAX][MAX][4];

vector<vi> bfs(int H, int W) {
    vector<vi> d(H, vi(W, INF));
    queue<pii> que;
    que.push(pii(H-1, W-1));
    d[H-1][W-1] = 0;
    while (!que.empty()) {
        auto p = que.front(); que.pop();
        int y = p.first, x = p.second;
        for (int i = 0; i < 4; i++) {
            if (!ok[y][x][i]) continue;
            int ny = y+dy[i], nx = x+dx[i];
            if (d[ny][nx] > d[y][x]+1) {
                d[ny][nx] = d[y][x]+1;
                que.push(pii(ny, nx));
            }
        }
    }
    return d;
}

vector<vi> dijkstra(int H, int W) {
    vector<vi> ret(H, vi(W, INF));
    ret[H-1][W-1] = 0;
    priority_queue<pair<int, pii> > que;
    que.push(make_pair(0, pii(H-1, W-1)));
    while (!que.empty()) {
        auto p = que.top(); que.pop();
        int dist = -p.first;
        int y = p.second.first, x = p.second.second;
        if (dist > ret[y][x]) continue;
        for (int i = 0; i < 4; i++) {
            if (!ok[y][x][i]) continue;
            int ny = y+dy[i], nx = x+dx[i];
            int tmp = max(dist+1, d[y][x][i][ny][nx]);
            if (ret[ny][nx] > tmp) {
                ret[ny][nx] = tmp;
                que.push(make_pair(-ret[ny][nx], pii(ny, nx)));
            }
        }
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    while (cin >> H >> W) {
        if (H==0 && W==0) break;
        memset(ok, false, sizeof(ok));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W-1; j++) {
                int b;
                cin >> b;
                if (b == 0) {
                    ok[i][j][0] = true;
                    ok[i][j+1][2] = true;
                }
            }
            if (i < H-1) {
                for (int j = 0; j < W; j++) {
                    int b;
                    cin >> b;
                    if (b == 0) {
                        ok[i][j][1] = true;
                        ok[i+1][j][3] = true;
                    }
                }
            }
        }
        vector<vi> d0 = bfs(H, W);
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
            for (int k = 0; k < 4; k++) {
                if (ok[i][j][k]) {
                    ok[i][j][k] = false;
                    d[i][j][k] = bfs(H, W);
                    ok[i][j][k] = true;
                }
            }
        }
        vector<vi> ans = dijkstra(H, W);
        int out = ans[0][0];
        if (out >= INF) out = -1;
        cout << out << endl;
    }
    return 0;
}

Measured memory_bytes: 18936