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 <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+1;
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<<endl;
}


	return 0;
 
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;

queue <int> qt;

int w,h;
int ydata[40][40];
int tdata[40][40];
bool fflag[40][40];
int tesuu[40][40];

void check(int px,int py){
  int dx[4]={-1,0,1,0};
  int dy[4]={0,-1,0,1};
  for(int i=0;i<4;i++){
    int nx=px+dx[i];
    int ny=py+dy[i];
    if((nx>-1)&&(ny>-1)&&(nx<h)&&(ny<w)){
      if(i==0){//up
        if((fflag[nx][ny]==false)&&(tdata[nx][ny]==0)){
          qt.push(100*nx+ny);
          fflag[nx][ny]=true;		
          tesuu[nx][ny]=tesuu[px][py]+1;
        }

      }
      if(i==1){//left
        if((fflag[nx][ny]==false)&&(ydata[nx][ny]==0)){
          qt.push(100*nx+ny);
          fflag[nx][ny]=true;
          tesuu[nx][ny]=tesuu[px][py]+1;
        }
      }
      if(i==2){//down
        if((fflag[nx][ny]==false)&&(tdata[nx-1][ny]==0)){
          qt.push(100*nx+ny);
          fflag[nx][ny]=true;		
          tesuu[nx][ny]=tesuu[px][py]+1;
        }
      }
      if(i==3){//right
        if((fflag[nx][ny]==false)&&(ydata[nx][ny-1]==0)){
          qt.push(100*nx+ny);
          fflag[nx][ny]=true;
          tesuu[nx][ny]=tesuu[px][py]+1;
        }
      }
    }
  }
  if(!qt.empty()){
    int t=qt.front();
    qt.pop();
    check(t/100,t%100);
  }
}

int main(){
  while(1){
    for(int i=0;i<40;i++){
      for(int j=0;j<40;j++){
        fflag[i][j]=false;
        tesuu[i][j]=0;
      }
    }
      cin >> w >> h;
    if((w==0)&&(h==0)) return 0;
    for(int i=0;i<h*2-1;i++){
      if(i%2==0){
        for(int j=0;j<w-1;j++){
          cin >> ydata[i/2][j];
        }
      }else{
        for(int j=0;j<w;j++){
          cin >> tdata[i/2][j];
        }
      }
    }
    fflag[0][0]=true;
    tesuu[0][0]=1;
    check(0,0);
    cout << tesuu[h-1][w-1] << endl;

  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
#define mp(a,b)	make_pair(a,b)
#define fst first
#define scn second


bool edge[50][50][4];
int dist[50][50];
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const int inf=1e9;
int main(){
	while(true){
		int w,h;	cin>>w>>h;
		if(w==0)	break;
		for(int i=0;i<50;i++)	for(int j=0;j<50;j++){
			for(int k=0;k<4;k++)	edge[i][j][k]=false;
			dist[i][j]=inf;
		}
		for(int i=1;i<w;i++){
			int c;	cin>>c;
			if(c==0)	edge[0][i-1][0]=edge[0][i][2]=true;
		}
		for(int i=1;i<h;i++){
			for(int j=0;j<w;j++){
				int c;	cin>>c;
				if(c==0)	edge[i-1][j][1]=edge[i][j][3]=true;
			}
			for(int j=1;j<w;j++){
				int c;	cin>>c;
				if(c==0)	edge[i][j-1][0]=edge[i][j][2]=true;
			}
		}
		dist[0][0]=1;
		priority_queue<pair<int,pii> > pq;
		pq.push(mp(-1,mp(0,0)));
		while(!pq.empty()){
			auto it=pq.top();	pq.pop();
			int d=-it.fst;
			int y=it.scn.fst;
			int x=it.scn.scn;
			if(dist[y][x]<d)	continue;
			d++;
			for(int i=0;i<4;i++){
				if(!edge[y][x][i])	continue;
				if(dist[y+dy[i]][x+dx[i]]>d){
					dist[y+dy[i]][x+dx[i]]=d;
					pq.push(mp(-d,mp(y+dy[i],x+dx[i])));
				}
			}
		}
		if(dist[h-1][w-1]==inf)	dist[h-1][w-1]=0;
		cout<<dist[h-1][w-1]<<endl;
	}
	return 0;
}