#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include<random>
#include<array>

using namespace std;

#define REP(i, n) for(int i = 0;i < n;++i)
#define REPR(i, n) for(int i = n-1;i >= 0;--i)
#define FOR(i, m, n) for(int i = m;i < n;++i)
#define FORR(i, m, n) for(int i = m-1;i >= n;--i)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb push_back
#define print(x) cout<<(x)<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define lb(v,n) lower_bound(v.begin(), v.end(), (n))
#define ub(v,n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i,v.size())cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7; const int MAX = 500020;
const double pi = acos(-1); const double EPS = 1e-8;
const ll INF = 1e12;

int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };

bool U[31][31], D[31][31], L[31][31], R[31][31];
int H, W;
bool visited[31][31];
int g(int x, int y) {
	return x * W + y;
}
int dist[31][31];
void solve() {
	cin >> W >> H;
	if (W == 0 && H == 0)exit(0);
	REP(i, H)REP(j, W) {
		dist[i][j] = 1e9;
		U[i][j] = D[i][j] = L[i][j] = R[i][j] = true;
	}
	//REP(i, H)REP(j, W) {
	//	cout << U[i][j] << D[i][j] << L[i][j] << R[i][j] << endl;
	//}
	dist[0][0] = 0;
	vector<vector<int>>ws(H, vector<int>(W - 1)), hs(H - 1, vector<int>(W));
	REP(_, 2 * H - 1) {
		if (_ % 2 == 0) {
			REP(i, W - 1) {
				int f; cin >> f;
				ws[_/2][i]=f;
			}
		}
		else {
			REP(i, W) {
				int f; cin >> f;
				hs[_/2][i]=f;
			}
		}
	}
	REP(i, H - 1) {
		REP(j, W) {
			if (hs[i][j] == 1) {
				D[i][j] = false;
				U[i + 1][j] = false;
			}
		}
	}
	REP(i, H) {
		REP(j, W - 1) {
			if (ws[i][j] == 1) {
				R[i][j] = false;
				L[i][j + 1] = false;
			}
		}
	}
	queue<P>que;
	que.push({ 0,0 });
	REP(i, H + 1)REP(j, W + 1)visited[i][j] = false;
	visited[0][0] = true;
	while (!que.empty()) {
		P p = que.front(); que.pop();
		int x = p.first, y = p.second;
		visited[x][y] = true;
		REP(k, 4) {
			int nx = x + dx[k], ny =y+ dy[k];
			//RLDU
			if (k == 0 && !R[x][y])continue;
			if (k == 1 && !L[x][y])continue;
			if (k == 2 && !D[x][y])continue;
			if (k == 3 && !U[x][y])continue;
			if (nx < 0 || nx >= H || ny < 0 || ny >= W)continue;
			if (visited[nx][ny])continue;
			chmin(dist[nx][ny], dist[x][y] + 1);
			que.push({ nx,ny });
			visited[nx][ny] = true;
		}
	}
	if (dist[H - 1][W - 1] < 1e9) {
		print(dist[H - 1][W - 1]+1);
	}
	else print(0);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q; cin >> q;
	//while (q--)
	while(true)
	solve();
}




Measured memory_bytes: 3204