#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

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 (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int W, H;
int Wall[2][40][40];
int dist[2][33][33][33][33];

int d[4][2]={{1, 0},{0, 1},{-1, 0},{0, -1}};
int e[4][2]={{0, 0},{1, 0},{0, 1},{1, 1}};

int memo[33][33];

int bfs(int x, int y){
	priority_queue<pipii, vector<pipii>, greater<pipii> > q;
	q.emplace(0, pii(H, W));
	memo[H][W] = 0;
	while(!q.empty()){
		int x=q.top().second.second;
		int y=q.top().second.first;
		q.pop();
		REP(di, 4){
			int dy = y+d[di][1];
			int dx = x+d[di][0];
			if(!Wall[e[di][0]][y+d[di][1]*e[di][1]][x+d[di][0]*e[di][1]]){
				int c = max(memo[y][x]+1, dist[e[di][0]][y+d[di][1]*e[di][1]][x+d[di][0]*e[di][1]][dy][dx]);
				if(memo[dy][dx] > c){
					memo[dy][dx] = c;
					q.emplace(c, pii(dy, dx));
				}
			}
		}
	}
	return memo[1][1];
}

main(){
	while(cin >> H >> W, H){
		memset(memo, 63, sizeof(memo));
		memset(Wall,  -1, sizeof(Wall));
		memset(dist, 63, sizeof(dist));
		int ans;
		REP(i, H-1){
			REP(j, W-1) cin >> Wall[0][i+1][j+1];
			REP(j, W)   cin >> Wall[1][i+1][j+1];
		}
		REP(j, W-1) cin >> Wall[0][H][j+1];
		REP(i, H+1)REP(j, W+1)REP(k, 2){
			if(Wall[k][i][j] && !(!k&&!i&&!j)) continue;
			Wall[k][i][j] = 1;
			
			dist[k][i][j][H][W] = 0;
			queue<pii> q;
			q.emplace(H, W);
			while(!q.empty()){
				int x=q.front().second;
				int y=q.front().first;
				q.pop();
				REP(di, 4){
					int dy = y+d[di][1];
					int dx = x+d[di][0];
					if(!Wall[e[di][0]][y+d[di][1]*e[di][1]][x+d[di][0]*e[di][1]] && dist[k][i][j][dy][dx] > dist[k][i][j][y][x]+1){
						dist[k][i][j][dy][dx] = dist[k][i][j][y][x]+1;
						q.push(pii(dy, dx));
					}
				}
			}
			
			Wall[k][i][j] = 0;
			
		}
		int a, b, c;
		memo[W][H] = 0;
		int res = bfs(W,H);
		cout << (res>900?-1:res) << endl;
	}
	return 0;
}

Measured memory_bytes: 10500