#include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

// UnionFind 解説：https://youtu.be/TdR816rqc3s?t=8424



int main() {
	vi ans;
	while (true) {
		int w, h, t[30][30];

		// 0: ↓, 1: →, 2: ↑, 3: ←
		// t[0][0][0] = 1 →　t[0][0]の上方向には壁がある
		vector<vector<vector<int>>> n(30, vector<vector<int>>(30, vector<int>(4)));
		
		cin >> w >> h;

		if (!w && !h) break;

		// 縦, 横
		vvi ver(h - 1), wid(h);
		rep(i, 2 * h - 1) {
			if (i % 2 == 0) {
				rep(j, w - 1) {
					int val;
					cin >> val;
					wid[i / 2].push_back(val);
				}
			}
			else {
				rep(j, w) {
					int val;
					cin >> val;
					ver[i / 2].push_back(val);
				}
			}
		}

		rep(i, h - 1) {
			rep(j, w) {
				if (ver[i][j]) n[i][j][0] = n[i + 1][j][2] = 1;
			}
		}

		rep(i, h) {
			rep(j, w - 1) {
				if (wid[i][j]) n[i][j][1] = n[i][j + 1][3] = 1;
			}
		}

		rep(i, h) {
			rep(j, w) {
				rep(k, 4) {
					int y = i + dy[k], x = j + dx[k];

					if (x < 0 || x >= w || y < 0 || y >= h) n[i][j][k] = 1;
				}
			}
		}

		n[0][0][2] = n[h - 1][w - 1][0] = 0;


		// BFS
		vvi depth(h, vi(w, -1));
		vvb check(h, vb(w));
		queue<ii> que;

		depth[0][0] = 0;
		check[0][0] = true;
		que.push({ 0, 0 });
		int ans_tmp = -1;
		while (!que.empty()) {
			ii now = que.front(); que.pop();

			if (now.first == h - 1 && now.second == w - 1) {
				//cout << now.first << " " << now.second << "       wawdawd" << endl;
				ans_tmp = depth[now.first][now.second] + 1;
			}

			rep(i, 4) {
				int y = now.first + dy[i], x = now.second + dx[i];

				if (0 <= x && x < w &&
					0 <= y && y < h &&
					check[y][x] == false &&
					n[now.first][now.second][i] == 0) {
					depth[y][x] = depth[now.first][now.second] + 1;
					check[y][x] = true;
					que.push({ y, x });
				}
			}
		}

		if (ans_tmp == -1) ans.push_back(0);
		else ans.push_back(ans_tmp);
	}
	rep(i, ans.size()) cout << ans[i] << endl;
}


Measured memory_bytes: 3136