#pragma GCC diagnostic ignored "-Wunused-variable" 
#include <bits/stdc++.h>
using namespace std;

//#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/range/adaptor/reversed.hpp>
using namespace boost;
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#define forir(i, ...) if(ll i=(ll)v.size())for(__VA_ARGS__)if(i--,1)
#define eachr(i, e, c) forir(i, const auto &e: adaptors::reverse(c))
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutd cout << fixed << setprecision(10)  // cout double
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...) if (ll i = -1) for(__VA_ARGS__) if (i++, 1)
#define each(i, e, c) fori (i, auto& e: c)       // for each
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
#define loop_end 9223372036854775806             // LLONG_MAX-1
/***** lambda *****/
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
(auto c) { return (ll)c - (ll)'0'; };
auto DivCeil = [] // if (a % b != 0) return a / b + 1;
(auto a, auto b) { return (ll)ceil((ld)a / (ld)b); };
auto LtoC = [] // number to char
(auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVT
(ll xs, vt<T>& v, T fix = T())
{	// input vector<T> (T != struct) + fix
	v.resize(xs); rep(i, 0, xs) { cin >> v[i]; v[i] += fix; }
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget
{
	/***** グリッド上でのBFS処理ライブラリ *****/

	struct GT_NyaaGridBFS
	{
		/**
		@brief 4方向BFS実行関数
		@param start 探索開始座標
		@param grid 入力グリッド
		@param wall 移動不可な値
		@param res 出力結果
		@note
		 探索開始頂点startから4方向BFSで処理する。
		**/
		template <class T> void Run4(const pair<long long, long long> start, const ll ymax, const ll xmax, const vector<vector<T>>& ywall, const vector<vector<T>>& xwall, const T wall, vector<vector<long long>>& res)
		{	// 初期化
			pair<long long, long long> ymove[2] = { {-1, 0}, {1, 0} };
			pair<long long, long long> xmove[2] = { {0, -1}, {0, 1} };
			pair<long long, long long> ymoveWall[2] = { {-1, 0}, {0, 0} };
			pair<long long, long long> xmoveWall[2] = { {0, -1}, {0, 0} };
			vector<vector<bool>> check(ymax, vector<bool>(xmax, false));
			check[start.first][start.second] = true;
			queue<pair<long long, long long>> q;
			q.push(start);
			// 初期化処理の追加
			res.resize(ymax); for (auto& e : res) e.resize(xmax);
			res[start.first][start.second] = 1;
			while (!q.empty())
			{
				long long i = -1;
				auto from = q.front(); q.pop();
				for (auto& m : ymove)
				{
					i++;
					long long ym = from.first + m.first;
					long long xm = from.second + m.second;
					if (ym < 0 || ymax <= ym || xm < 0 || xmax <= xm) continue;
					// 移動不可座標の処理
					long long yw = from.first + ymoveWall[i].first;
					long long xw = from.second + ymoveWall[i].second;
					if (yw < 0 || ymax - 1 <= yw || xw < 0 || xmax <= xw) continue;
					if (ywall[yw][xw] == wall) continue;
					// 移動可能座標の処理
					if (check[ym][xm]) continue;
					else check[ym][xm] = true;
					res[ym][xm] = res[from.first][from.second] + 1;
					q.push({ ym, xm });
				}
				i = -1;
				for (auto& m : xmove)
				{
					i++;
					long long ym = from.first + m.first;
					long long xm = from.second + m.second;
					if (ym < 0 || ymax <= ym || xm < 0 || xmax <= xm) continue;
					// 移動不可座標の処理
					long long yw = from.first + xmoveWall[i].first;
					long long xw = from.second + xmoveWall[i].second;
					if (yw < 0 || ymax <= yw || xw < 0 || xmax - 1 <= xw) continue;
					if (xwall[yw][xw] == wall) continue;
					// 移動可能座標の処理
					if (check[ym][xm]) continue;
					else check[ym][xm] = true;
					res[ym][xm] = res[from.first][from.second] + 1;
					q.push({ ym, xm });
				}
			}
		}

		/**
		@brief 6方向BFS実行関数
		@param start 探索開始座標
		@param grid 入力グリッド
		@param wall 移動不可な値
		@param res 出力結果
		@note
		 探索開始頂点startから6方向BFSで処理する。
		**/
		template <class T> void Run6(pair<long long, long long> start, const vector<vector<T>>& grid, T wall, vector<vector<long long>>& res)
		{	// 初期化
			pair<long long, long long> move1[6] = { {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, 0}, {1, 1} };
			pair<long long, long long> move2[6] = { {-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 0} };
			vector<vector<bool>> check(grid.size(), vector<bool>(grid[0].size(), false));
			check[start.first][start.second] = true;
			queue<pair<long long, long long>> q;
			q.push(start);
			// 初期化処理の追加
			res.resize(grid.size()); for (auto& e : res) e.resize(grid[0].size());
			while (!q.empty())
			{
				auto from = q.front(); q.pop();
				if (from.first % 2 != 0)
				{
					for (auto& m : move1)
					{
						long long y = from.first + m.first;
						long long x = from.second + m.second;
						if (y < 0 || (long long)grid.size() <= y || x < 0 || (long long)grid[y].size() <= x) continue;
						// 移動不可座標の処理
						if (grid[y][x] == wall) continue;
						// 移動可能座標の処理
						if (check[y][x]) continue;
						else check[y][x] = true;
						q.push({ y, x });
					}
				}
				else
				{
					for (auto& m : move2)
					{
						long long y = from.first + m.first;
						long long x = from.second + m.second;
						if (y < 0 || (long long)grid.size() <= y || x < 0 || (long long)grid[y].size() <= x) continue;
						// 移動不可座標の処理
						if (grid[y][x] == wall) continue;
						// 移動可能座標の処理
						if (check[y][x]) continue;
						else check[y][x] = true;
						q.push({ y, x });
					}
				}
			}
		}

		/**
		@brief 8方向BFS実行関数
		@param start 探索開始座標
		@param grid 入力グリッド
		@param wall 移動不可な値
		@param res 出力結果
		@note
		 探索開始頂点startから8方向BFSで処理する。
		**/
		template <class T> void Run8(pair<long long, long long> start, const vector<vector<T>>& grid, T wall, vector<vector<long long>>& res)
		{	// 初期化
			pair<long long, long long> move[8] = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
			vector<vector<bool>> check(grid.size(), vector<bool>(grid[0].size(), false));
			check[start.first][start.second] = true;
			queue<pair<long long, long long>> q;
			q.push(start);
			// 初期化処理の追加
			res.resize(grid.size()); for (auto& e : res) e.resize(grid[0].size());
			while (!q.empty())
			{
				auto from = q.front(); q.pop();
				for (auto& m : move)
				{
					long long y = from.first + m.first;
					long long x = from.second + m.second;
					if (y < 0 || (long long)grid.size() <= y || x < 0 || (long long)grid[y].size() <= x) continue;
					// 移動不可座標の処理
					if (grid[y][x] == wall) continue;
					// 移動可能座標の処理
					if (check[y][x]) continue;
					else check[y][x] = true;
					q.push({ y, x });
				}
			}
		}
	};
}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = NT_ModLL< 1000000007 >;
//using mll = NT_ModLL< 998244353 >;

int main(void)
{
	ll w, h;
	vt<ll> ans;
	while (true)
	{
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		vvt<ll> xwall; MakeVVT(h + 1, w - 1, xwall);
		vvt<ll> ywall; MakeVVT(h, w, ywall);
		rep(y, 0, h)
		{
			rep(x, 0, w - 1) cin >> xwall[y][x];
			if (y == h - 1) continue;
			rep(x, 0, w) cin >> ywall[y][x];
		}
		vvt<ll> res;
		GT_NyaaGridBFS bfs;
		bfs.Run4({ 0,0 }, h, w, ywall, xwall, 1LL, res);
		ans.push_back(res.back().back());
	}
	each(i, e, ans) cout << e << endl;
	return 0;
}



Measured memory_bytes: 3100