//#include <tourist>
#include <bits/stdc++.h>
/*
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <tuple>
#include <deque>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <utility>
#include <complex>
#include <functional>
*/
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
//setprecision(15)有効数字15桁
//-std=c++14
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);
}
struct edge{ll to, cost;};
typedef pair<ll,ll> P;
struct graph{
  ll V;
  vector<vector<edge> > G;
  vector<ll> d;
//vector<vector<int> > prev((int)G.size()); // prev[v] := v から復元できる辺たち
  graph(ll n){
    init(n);
  }

  void init(ll n){
    V = n;
    G.resize(V);
    d.resize(V);
    REP(i,V){
      d[i] = INF;
    }
  }

  void add_edge(ll s, ll t, ll cost){
    edge e;
    e.to = t; e.cost = cost;
    G[s].push_back(e);
  }

  void dijkstra(ll s){
    REP(i,V){
      d[i] = INF;
    }
    d[s] = 0;
    priority_queue<P,vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
      P p = que.top(); que.pop();
      ll v = p.second;
      if(d[v]<p.first) continue;
      for(int i=0;i<G[v].size();i++){
          edge e=G[v][i];
        if(d[e.to]>d[v]+e.cost){
          d[e.to] = d[v]+e.cost;
          que.push(P(d[e.to],e.to));
          // 復元のための
          //prev[e.first].clear();
          //prev[e.first].push_back(v);
        }
      }
    }
  }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<int> ans;
    while(true){
        int h, w;
        cin>>w>>h;
        if(h==0&&w==0){
            break;
        }
        graph g(h*w);
        for(int i=0;i<h-1;i++){
            for(int j=0;j<w-1;j++){
                int temp;
                cin>>temp;
                if(!temp){
                    g.add_edge(i*w+j,i*w+j+1,1);
                    g.add_edge(i*w+j+1,i*w+j,1);
                }
            }
            for(int j=0;j<w;j++){
                int temp;
                cin>>temp;
                if(!temp){
                    g.add_edge(i*w+j,(i+1)*w+j,1);
                    g.add_edge((i+1)*w+j,i*w+j,1);
                }
            }
        }
        for(int j=0;j<w-1;j++){
                int temp;
                cin>>temp;
                if(!temp){
                    g.add_edge((h-1)*w+j,(h-1)*w+j+1,1);
                    g.add_edge((h-1)*w+j+1,(h-1)*w+j,1);
                }
        }
        g.dijkstra(0);
        if(g.d[h*w-1]==INF){
            ans.push_back(0);
        }
        else{
            ans.push_back(g.d[h*w-1]+1);
        }
    }
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<"\n";
    }
}



Measured memory_bytes: 3132