#include <bits/stdc++.h>
#define trace1(a)                    cout<<#a<<": "<<a<<endl
#define trace2(a,b)                  cout<<#a<<": "<<a<<" | ", trace1(b)
#define trace3(a,b,c)                cout<<#a<<": "<<a<<" | ", trace2(b,c)
#define trace4(a,b,c,d)              cout<<#a<<": "<<a<<" | ", trace3(b,c,d)
#define trace5(a,b,c,d,e)            cout<<#a<<": "<<a<<" | ", trace4(b,c,d,e)
#define trace6(a,b,c,d,e,f)          cout<<#a<<": "<<a<<" | ", trace5(b,c,d,e,f)
#define trace7(a,b,c,d,e,f,g)        cout<<#a<<": "<<a<<" | ", trace6(b,c,d,e,f,g)
#define trace8(a,b,c,d,e,f,g,h)      cout<<#a<<": "<<a<<" | ", trace7(b,c,d,e,f,g,h)
#define trace9(a,b,c,d,e,f,g,h,i)    cout<<#a<<": "<<a<<" | ", trace8(b,c,d,e,f,g,h,i)
#define trace10(a,b,c,d,e,f,g,h,i,j) cout<<#a<<": "<<a<<" | ", trace9(b,c,d,e,f,g,h,i,j)
#define rep(i,a,b)      for(int (i)=(int)(a);(i)<(int)(b); ++(i))
#define reps(i,a,b,c)   for(int (i)=(int)(a);(i)<(int)(b); (i)+=(c))
#define rrep(i,a,b)     for(int (i)=(int)(a);(i)>=(int)(b);--(i))
#define rreps(i,a,b,c)  for(int (i)=(int)(a);(i)>=(int)(b);(i)-=(c))
#define fore(x,a)       for(auto &x:a)
#define foreach(i,a)    for(auto i=(a).begin(); i!=(a).end(); ++i)
#define rforeach(i,a)   for(auto i=(a).rbegin();i!=(a).rend();++i)
#define all(a)  (a).begin(), (a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define isin(i,a,b) ((a) <= (i) && (i) <= (b))
#define uni(a) (a).erase(unique(all(a)),(a).end())
#define dup(x,y) (((x)+(y)-1)/(y))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(a) ((long long)(a).size())
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll  = long long;          using vi  = vector<int>;
using ld  = long double;        using vl  = vector<ll>;
using unit= unsigned;           using vvi = vector<vi>;
using ull = unsigned long long; using vvl = vector<vl>;
using pii = pair<int,int>;      using vpii= vector<pii>;
using pil = pair<int,ll>;       using vpil= vector<pil>;
using pli = pair<ll,int>;       using vpli= vector<pli>;
using pll = pair<ll,ll>;        using vpll= vector<pll>;
void _main(); int main(){ cin.tie(0); ios::sync_with_stdio(false); _main(); }
template<class T>string join(const v(T)&v){ stringstream s; rep(i,0,sz(v))s<<' '<<v[i]; return s.str().substr(1); }
template<class T>istream &operator>>(istream&i, v(T)&v){ fore(x,v){ i >> v; } return i; }
template<class T>ostream &operator<<(ostream&o, const v(T)&v){ o<<"["; fore(x,v)o<<x<<","; o<<"]"; return o; }
template<class T>ostream &operator<<(ostream&o, const deque<T>&v){ o<<"deq["; fore(x,v)o<<x<<","; o<<"]"; return o; }
template<class T>ostream &operator<<(ostream&o, const set<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const unordered_set<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const multiset<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const unordered_multiset<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T1,class T2>ostream &operator<<(ostream &o, const pair<T1,T2>&p){ o<<"("<<p.fi<<","<<p.se<<")"; return o; }
template<class TK,class TV>ostream &operator<<(ostream &o, const map<TK,TV>&m){ o<<"{"; fore(x,m)o<<x.fi<<"=>"<<x.se<<","; o<<"}"; return o; }
template<class TK,class TV>ostream &operator<<(ostream &o, const unordered_map<TK,TV>&m){ o<<"{"; fore(x,m)o<<x.fi<<"=>"<<x.se<<","; o<<"}"; return o; }
template<class T>void YES(T c){ if(c) cout<<"YES"<<endl; else cout<<"NO"<<endl; }
template<class T>void Yes(T c){ if(c) cout<<"Yes"<< endl; else cout<<"No"<<endl; }
template<class T>void POSS(T c){ if(c) cout<<"POSSIBLE"<<endl; else cout<<"IMPOSSIBLE"<<endl; }
template<class T>void Poss(T c){ if(c) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; }
template<class T>bool chmax(T &a,const T &b){ return a<b?(a=b,true):false; }
template<class T>bool chmin(T &a,const T &b){ return a>b?(a=b,true):false; }
template<class T>T gcd(T a,T b){ return b?gcd(b,a%b):a; }
template<class T>T lcm(T a,T b){ return a/gcd(a,b)*b; }
const double  EPS = 1e-10;
const double  PI  = acos(-1.0);
const int     INF = 1001002003;
const ll      LINF= 1001002003004005006LL;
// const int DX[] = { -1, 0, 1, 0 , -1,-1, 1, 1 };
// const int DY[] = {  0, 1, 0,-1 , -1, 1,-1, 1 };
const int DX[] = { -1, 0, 1, 0 };
const int DY[] = {  0, 1, 0,-1 };

struct Tile {
  bool wall[4];
};

template<typename T>
vector<vector<int>> board_distance(
    const vector<vector<T>>& board, int sx, int sy, int gx, int gy) {
  // const int DX[] = { -1, 0, 1, 0 , -1,-1, 1, 1 };
  // const int DY[] = {  0, 1, 0,-1 , -1, 1,-1, 1 };
  const int dn = 4;
  int h = board.size();
  int w = board[0].size();
  vector<vector<int>> ret(h, vector<int>(w,-1));
  ret[sx][sy] = 0;
  queue<pair<int,int>> q;
  q.emplace(sx,sy);
  int step = 0;
  while (!q.empty()) {
    ++step;
    for (int qi=q.size(); qi>=1; --qi) {
      pair<int,int> p = q.front(); q.pop();
      int x=p.first, y=p.second;
      if (x == gx && y == gy) {
        queue<pair<int,int>> empty;
        swap(q, empty);
        break;
      }
      for (int d=0; d<dn; ++d) {
        int nx=x+DX[d], ny=y+DY[d];
        if (nx<0 || h<=nx || ny<0 || w<=ny) continue;
        if (ret[nx][ny] != -1) continue;
        // trace4(board[x][y].wall[0], board[x][y].wall[1], board[x][y].wall[2], board[x][y].wall[3]);
        // trace6(x, y, nx, ny, d, board[x][y].wall[d]);
        if (board[x][y].wall[d]) continue;
        ret[nx][ny] = step;
        q.emplace(nx, ny);
      }
    }
  }
  return ret;
}

void _main() {
  vi ans;
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    vv(Tile) a(h, v(Tile)(w));
    rep(i,0,2*h-1) {
      int hi = i/2;
      if (i&1) {
        vi b(w);
        fore(x,b) cin >> x;
        rep(j,0,w) {
          a[hi][j].wall[2] = b[j];
          if (hi != h-1) a[hi+1][j].wall[0] = b[j];
        }
      } else {
        vi b(w-1);
        fore(x,b) cin >> x;
        rep(j,0,w-1) {
          a[hi][j].wall[1]  = b[j];
          a[hi][j+1].wall[3]= b[j];
        }
      }
    }
    vvi dist = board_distance(a, 0, 0, h-1, w-1);
    ans.pb(dist[h-1][w-1] + 1);
  }
  fore(x,ans) cout << x << endl;
}



Measured memory_bytes: 3188