// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>   
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(ll i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) rep(i,v.size())cout<<" "<<v[i];cout<<endl<<endl;
#define show2d(v) rep(i,v.size()){rep(j,v[i].size())cout<<" "<<v[i][j];cout<<endl;}cout<<endl;
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int n,m;
vvi H,W;
vvi dp,T;

int main(){
	while(cin>>n>>m,n){
		H=vvi(n-1,vi(m));
		W=vvi(n,vi(m-1));
		rep(k,n-1){
			rep(i,m-1)cin>>W[k][i];
			rep(i,m)cin>>H[k][i];
		}
		rep(i,m-1)cin>>W[n-1][i];
		T=dp=vvi(n,vi(m));
		rep(i,n-1)rep(j,m)if(H[i][j]==0){
			H[i][j]=1;
			
			rep(x,n)rep(y,m)T[x][y]=inf;
			T[n-1][m-1]=0;
			queue<pii>q;
			q.push({n-1,m-1});
			while(!q.empty()){
				pii t=q.front();
				q.pop();
				rep(w,4){
					int x=t.first+dx[w];
					int y=t.second+dy[w];
					if(x<0||y<0||x>=n||y>=m)continue;
					if(w==0&&W[t.first][t.second])continue;
					if(w==1&&H[t.first][t.second])continue;
					if(w==2&&W[t.first][t.second-1])continue;
					if(w==3&&H[t.first-1][t.second])continue;
					if(T[x][y]>T[t.first][t.second]+1){
						T[x][y]=T[t.first][t.second]+1;
						q.push({x,y});
					}
				}
			}
			rep(x,n)rep(y,m)dp[x][y]=max(dp[x][y],T[x][y]);
			H[i][j]=0;
		}
		rep(i,n)rep(j,m-1)if(W[i][j]==0){
			W[i][j]=1;
			
			rep(x,n)rep(y,m)T[x][y]=inf;
			T[n-1][m-1]=0;
			queue<pii>q;
			q.push({n-1,m-1});
			while(!q.empty()){
				pii t=q.front();
				q.pop();
				rep(w,4){
					int x=t.first+dx[w];
					int y=t.second+dy[w];
					if(x<0||y<0||x>=n||y>=m)continue;
					if(w==0&&W[t.first][t.second])continue;
					if(w==1&&H[t.first][t.second])continue;
					if(w==2&&W[t.first][t.second-1])continue;
					if(w==3&&H[t.first-1][t.second])continue;
					if(T[x][y]>T[t.first][t.second]+1){
						T[x][y]=T[t.first][t.second]+1;
						q.push({x,y});
					}
				}
			}
			rep(x,n)rep(y,m)dp[x][y]=max(dp[x][y],T[x][y]);
			W[i][j]=0;
		}
		rep(i,n)rep(j,m)T[i][j]=inf;
		T[0][0]=dp[0][0];
		priority_queue<tp>q;
		q.push(tp(0,0,0));
		while(!q.empty()){
			int x,y,cost;
			tie(cost,x,y)=q.top();
			cost*=-1;
			q.pop();
			rep(w,4){
				int nx=x+dx[w];
				int ny=y+dy[w];
				if(nx<0||ny<0||nx>=n||ny>=m)continue;
				if(w==0&&W[x][y])continue;
				if(w==1&&H[x][y])continue;
				if(w==2&&W[x][y-1])continue;
				if(w==3&&H[x-1][y])continue;
				int ncost=max(T[x][y],cost+1+dp[nx][ny]);
				if(T[nx][ny]>ncost){
					T[nx][ny]=ncost;
					q.push(tp(-cost-1,nx,ny));
				}
			}
		}
//		show2d(dp);
//		show2d(T);
		if(T[n-1][m-1]==inf)T[n-1][m-1]=-1;
		cout<<T[n-1][m-1]<<endl;
	}
}
















Measured memory_bytes: 3140