#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 31;

int h,w;
int tate[MAX_N][MAX_N];
int yoko[MAX_N][MAX_N];
int tt[MAX_N][MAX_N][2];
int yk[MAX_N][MAX_N][2];
int d[MAX_N*MAX_N];

bool possible(int cri)
{
    rep(i,h){
        rep(j,w){
            d[i*w+j] = INF;
        }
    }
    queue<int> que;
    que.push(0);
    d[0] = 0;
    while(!que.empty()){
        int p = que.front();
        que.pop();
        int x = p / w, y = p % w;
        if(y < w-1 && !yoko[x][y] && d[x*w+y+1] > d[p] + 1 && d[p] + yk[x][y][0] <= cri){
            d[x*w+y+1] = d[p] + 1;
            que.push(x*w+y+1);
        }
        if(y > 0 && !yoko[x][y-1] && d[x*w+y-1] > d[p] + 1 && d[p] + yk[x][y-1][1] <= cri){
            d[x*w+y-1] = d[p] + 1;
            que.push(x*w+y-1);
        }
        if(x < h-1 && !tate[x][y] && d[(x+1)*w+y] > d[p] + 1 && d[p] + tt[x][y][0] <= cri){
            d[(x+1)*w+y] = d[p] + 1;
            que.push((x+1)*w+y);
        }
        if(x > 0 && !tate[x-1][y] && d[(x-1)*w+y] > d[p] + 1 && d[p] + tt[x-1][y][1] <= cri){
            d[(x-1)*w+y] = d[p] + 1;
            que.push((x-1)*w+y);
        }
    }
    return (d[h*w-1] != INF);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin >> h >> w;
        if(h == 0 && w == 0){
            break;
        }
        rep(i,2*h-1){
            if(i % 2){
                rep(j,w){
                    cin >> tate[i/2][j];
                }
            }else{
                rep(j,w-1){
                    cin >> yoko[i/2][j];
                }
            }
        }
        rep(i,h){
            rep(j,w-1){
                if(!yoko[i][j]){
                    yoko[i][j] = 1;
                    rep(k,2){
                        int pos = w*i+j+k;
                        rep(l,h){
                            rep(r,w){
                                d[l*w+r] = INF;
                            }
                        }
                        queue<int> que;
                        d[pos] = 0;
                        que.push(pos);
                        while(!que.empty()){
                            int p = que.front();
                            que.pop();
                            int x = p / w, y = p % w;
                            if(y < w-1 && !yoko[x][y] && d[x*w+y+1] > d[p] + 1){
                                d[x*w+y+1] = d[p] + 1;
                                que.push(x*w+y+1);
                            }
                            if(y > 0 && !yoko[x][y-1] && d[x*w+y-1] > d[p] + 1){
                                d[x*w+y-1] = d[p] + 1;
                                que.push(x*w+y-1);
                            }
                            if(x < h-1 && !tate[x][y] && d[(x+1)*w+y] > d[p] + 1){
                                d[(x+1)*w+y] = d[p] + 1;
                                que.push((x+1)*w+y);
                            }
                            if(x > 0 && !tate[x-1][y] && d[(x-1)*w+y] > d[p] + 1){
                                d[(x-1)*w+y] = d[p] + 1;
                                que.push((x-1)*w+y);
                            }
                        }
                        yk[i][j][k] = d[h*w-1];
                    }
                    yoko[i][j] = 0;
                }
            }
        }
        rep(i,h-1){
            rep(j,w){
                if(!tate[i][j]){
                    tate[i][j] = 1;
                    rep(k,2){
                        int pos = w*(i+k)+j;
                        rep(l,h){
                            rep(r,w){
                                d[l*w+r] = INF;
                            }
                        }
                        queue<int> que;
                        d[pos] = 0;
                        que.push(pos);
                        while(!que.empty()){
                            int p = que.front();
                            que.pop();
                            int x = p / w, y = p % w;
                            if(y < w-1 && !yoko[x][y] && d[x*w+y+1] > d[p] + 1){
                                d[x*w+y+1] = d[p] + 1;
                                que.push(x*w+y+1);
                            }
                            if(y > 0 && !yoko[x][y-1] && d[x*w+y-1] > d[p] + 1){
                                d[x*w+y-1] = d[p] + 1;
                                que.push(x*w+y-1);
                            }
                            if(x < h-1 && !tate[x][y] && d[(x+1)*w+y] > d[p] + 1){
                                d[(x+1)*w+y] = d[p] + 1;
                                que.push((x+1)*w+y);
                            }
                            if(x > 0 && !tate[x-1][y] && d[(x-1)*w+y] > d[p] + 1){
                                d[(x-1)*w+y] = d[p] + 1;
                                que.push((x-1)*w+y);
                            }
                        }
                        tt[i][j][k] = d[h*w-1];
                    }
                    tate[i][j] = 0;
                }
            }
        }
        int l = 0, r = 2*w*h-h-w;
        if(!possible(r)){
            cout << "-1\n";
            continue;
        }
        while(r-l>1){
            int mid = (l+r)/2;
            if(possible(mid)){
                r = mid;
            }else{
                l = mid;
            }
        }
        cout << r << "\n";
    }
    return 0;
}



Measured memory_bytes: 3212