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 <bits/stdc++.h>
using namespace std;

const int MAX_HW=35;

int w,h;
vector<vector<int>> mw(MAX_HW,vector<int>(MAX_HW)),mh(MAX_HW,vector<int>(MAX_HW)),d(MAX_HW,vector<int>(MAX_HW));
queue<pair<int,int>> que;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(cin >> w >> h,w){
		for (int i=0;i<2*h-1;++i){
			if (!(i&1)) for (int j=0;j<w-1;++j){
				cin >> mw[i/2][j];
			} else for (int j=0;j<w;++j){
				cin >> mh[(i-1)/2][j];
			}
		}
		for (int i=0;i<h;++i)
			for (int j=0;j<w;++j)
				d[i][j]=-1;
		d[0][0]=1;
		que.emplace(0,0);
		while(!que.empty()){
			auto p=que.front(); que.pop();
			int x=p.first,y=p.second;
			if (y<w-1&&!mw[x][y]&&d[x][y+1]==-1){
				d[x][y+1]=d[x][y]+1;
				que.emplace(x,y+1);
			}
			if (0<y&&!mw[x][y-1]&&d[x][y-1]==-1){
				d[x][y-1]=d[x][y]+1;
				que.emplace(x,y-1);
			}
			if (x<h-1&&!mh[x][y]&&d[x+1][y]==-1){
				d[x+1][y]=d[x][y]+1;
				que.emplace(x+1,y);
			}
			if (0<x&&!mh[x-1][y]&&d[x-1][y]==-1){
				d[x-1][y]=d[x][y]+1;
				que.emplace(x-1,y);
			}
		}
		cout << (d[h-1][w-1]!=-1?d[h-1][w-1]:0) << '\n';
	}
}

C++: #include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

#define rep(X,Y) for(int (X) = 0; (X) < (Y) ; ++(X))
#define all(X) (X).begin,(X).end
#define reps(i,j,k) for(inti=(j); i <= (k); ++(i))
#define szh size()
#define in(i,j,k) ((i) >= (j) && (i) <= (k))

using namespace std;

typedef pair<int,int> pii;

template<class S,class T>ostream& operator<<(ostream &os,const pair<S,T> &t){return os<<"("<<t.X<<","<<t.Y<<")";}

int walls[2][33][33],
  dx[]={1,0,-1,0},dy[]={0,1,0,-1},
  wtype[4]={0,1,0,1},wx[]={0,0,-1,0},wy[]={0,0,0,-1},
  dist[33][33];
const int INF=1e9;

int main(){
  int n,m;
  while(cin>>m>>n,m){
    memset(walls,0,sizeof(walls));
    fill(dist[0],dist[0]+33*33,INF);
    rep(i,n-1)rep(t,2)rep(j,m-1+t)
      cin>>walls[t][i][j];
    rep(j,m-1)
      cin>>walls[0][n-1][j];
    queue<pii> que;
    que.emplace(0,0);
    dist[0][0]=1;
    while(!que.empty()){
      pii cur=que.front(); que.pop();
      //cout<<cur<<endl;
      rep(d,4){
	pii nxt=cur;
	int nd=dist[cur.Y][cur.X]+1;
	nxt.X+=dx[d]; nxt.Y+=dy[d];
	if(nxt.X<0 || m<=nxt.X || nxt.Y<0 || n<=nxt.Y) continue;
	//cout<<nxt<<endl;
	if(walls[wtype[d]][cur.Y+wy[d]][cur.X+wx[d]]) continue;
	//cout<<nxt<<"!"<<endl;
	if(dist[nxt.Y][nxt.X]>nd){
	  dist[nxt.Y][nxt.X]=nd;
	  que.push(nxt);
	}
      }
    }
    cout<<(dist[n-1][m-1]==INF?0:dist[n-1][m-1])<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
using namespace std;
bool wall[2][35][35];
int dis[35][35];
queue<pair<int,int> > q1;

int main(){
	int n,m;
	int i,j,k;
	int a,b,c;
	while(cin>>m>>n){
		if(!n)break;
		for(i=0;i<2*n-1;i++){
			for(j=0;j<m-1+i%2;j++){
				cin>>a;
				if(a==1)wall[i%2][i/2][j]=true;
				else wall[i%2][i/2][j]=false;
			}
		}
		memset(dis,0,sizeof(dis));
		q1.push(make_pair(0,0));
		for(i=1;!q1.empty();i++){
			for(j=q1.size();j>0;j--){
				a=q1.front().first,b=q1.front().second,q1.pop();
				if(a<0 || n<=a || b<0 || m<=b)continue;
				if(dis[a][b])continue;
				dis[a][b]=i;
				if(!wall[0][a][b])q1.push(make_pair(a,b+1));
				if(b>0 && !wall[0][a][b-1])q1.push(make_pair(a,b-1));
				if(!wall[1][a][b])q1.push(make_pair(a+1,b));
				if(a>0 && !wall[1][a-1][b])q1.push(make_pair(a-1,b));
			}
		}
		cout<<dis[n-1][m-1]<<endl;
	}
}