Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		w, h := io.NextInt(), io.NextInt()
		if w == 0 {
			break
		}
		xWall := make([][]int, h)
		yWall := make([][]int, h)
		for i := 0; i < h; i++ {
			xWall[i], yWall[i] = make([]int, w), make([]int, w)
			for j := 0; j < w; j++ {
				xWall[i][j], yWall[i][j] = -1, -1
			}
		}
		for i := 0; i < h*2-1; i++ {
			if i%2 == 0 {
				for j := 0; j < w-1; j++ {
					xWall[i/2][j] = io.NextInt()
				}
			} else {
				for j := 0; j < w; j++ {
					yWall[i/2][j] = io.NextInt()
				}
			}
		}
		//io.Println(bfs(w, h, xWall, yWall))
		fmt.Println(bfs(w, h, xWall, yWall))
	}
}

type P struct {
	x, y int
}

func bfs(w, h int, x, y [][]int) int {
	d := make([][]int, h)
	for i := 0; i < h; i++ {
		d[i] = make([]int, w)
	}
	q := list.New()
	q.PushBack(P{0, 0})
	d[0][0] = 1

	for q.Len() != 0 {
		pos := q.Remove(q.Front()).(P)
		if pos.x == w-1 && pos.y == h-1 {
			break
		}
		if pos.x > 0 && x[pos.y][pos.x-1] == 0 && d[pos.y][pos.x-1] == 0 {
			d[pos.y][pos.x-1] = d[pos.y][pos.x] + 1
			q.PushBack(P{pos.x - 1, pos.y})
		}
		if pos.x < w-1 && x[pos.y][pos.x] == 0 && d[pos.y][pos.x+1] == 0 {
			d[pos.y][pos.x+1] = d[pos.y][pos.x] + 1
			q.PushBack(P{pos.x + 1, pos.y})
		}
		if pos.y > 0 && y[pos.y-1][pos.x] == 0 && d[pos.y-1][pos.x] == 0 {
			d[pos.y-1][pos.x] = d[pos.y][pos.x] + 1
			q.PushBack(P{pos.x, pos.y - 1})
		}
		if pos.y < h-1 && y[pos.y][pos.x] == 0 && d[pos.y+1][pos.x] == 0 {
			d[pos.y+1][pos.x] = d[pos.y][pos.x] + 1
			q.PushBack(P{pos.x, pos.y + 1})
		}
	}
	return d[h-1][w-1]
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: //#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";
    }
}

C++: 

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
 
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,-1,1,-1,1};
 
 
int main() {
int tate,yoko;
while(cin>>yoko>>tate && yoko){
int t[35][35]={0},y[35][35]={0};
for(int i=0;i<2*tate-1;i++){
if(i%2==0){
for(int j=0;j<yoko-1;j++) cin>>y[i/2][j];
}
else{
for(int j=0;j<yoko;j++) cin>>t[i/2][j];
}
}
int su[35][35]={0};
queue<pair<pair<int,int>,int > > qu;
qu.push(make_pair(make_pair(0,0),0));
su[0][0]=1;
int ans;
while(1){
if(qu.size()==0){
ans=-1;
break;
}
pair<pair<int,int>,int > dd=qu.front();
qu.pop();
int t1=dd.first.first,y1=dd.first.second;
if(dd.first.first==tate-1 && dd.first.second==yoko-1){
ans=dd.second;
break;
}
for(int i=0;i<4;i++){
if(0>t1+dx[i] || t1+dx[i]>=tate || 0>y1+dy[i] || y1+dy[i]>=yoko) continue;
if((i==0 && su[t1+dx[i]][y1+dy[i]]==0 && t[t1][y1]==0) ||(i==1 && su[t1+dx[i]][y1+dy[i]]==0 && t[t1+dx[i]][y1+dx[i]]==0)||(i==2 && su[t1+dx[i]][y1+dy[i]]==0 && y[t1][y1]==0) || (i==4 && su[t1+dx[i]][y1+dy[i]]==0 && y[t1+dx[i]][y1+dy[i]]==0) ){
qu.push(make_pair(make_pair(t1+dx[i],y1+dy[i]),dd.second+1));
su[t1+dx[i]][y1+dy[i]]=1;
}

}



}

cout<<ans+1<<endl;
}