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<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

int main(){

  int w,h;
  cin >> w >> h;

  while(true){
    if(w==0 && h==0)break;
    //eが縦棒、fが横棒
    int e[h][w+1];
    int f[h+1][w];
    //周りを囲む
    for(int i=0;i<h;i++){
      e[i][0]=1;
      e[i][w]=1;
    }
    for(int i=0;i<w;i++){
      f[0][i]=1;
      f[h][i]=1;
    }
    //入力
    for(int i=0;i<h-1;i++){
      for(int j=1;j<w;j++){
        cin >> e[i][j];
      }
      for(int j=0;j<w;j++){
        cin >> f[i+1][j];
      }
    }
    for(int i=1;i<w;i++){
      cin >> e[h-1][i];
    } 

    int dist[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        dist[i][j]=-1;
      }
    }

    queue<vector<int>> que;

    dist[0][0]=1;
    que.push({0,0});

    while(!que.empty()){
      vector<int> v=que.front();
      que.pop();

      if(v[0]+1<h && f[v[0]+1][v[1]]==0 && dist[v[0]+1][v[1]]==-1){
        dist[v[0]+1][v[1]]=dist[v[0]][v[1]]+1;
        que.push({v[0]+1,v[1]});
      }

      if(v[0]-1>-1 && f[v[0]][v[1]]==0 && dist[v[0]-1][v[1]]==-1){
        dist[v[0]-1][v[1]]=dist[v[0]][v[1]]+1;
        que.push({v[0]-1,v[1]});
      }

      if(v[1]-1>-1 && e[v[0]][v[1]]==0 && dist[v[0]][v[1]-1]==-1){
        dist[v[0]][v[1]-1]=dist[v[0]][v[1]]+1;
        que.push({v[0],v[1]-1});
      }

      if(v[1]+1<w && e[v[0]][v[1]+1]==0 && dist[v[0]][v[1]+1]==-1){
        dist[v[0]][v[1]+1]=dist[v[0]][v[1]]+1;
        que.push({v[0],v[1]+1});
      }     

    }

    if(dist[h-1][w-1]!=-1)cout << dist[h-1][w-1] << endl;
    else cout << 0 << endl;


    cin >> w >> h;

  }

}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };
int main(){
	int w, h;
	while( cin >> w >> h && w && h ){
		int maze[65][65];
		// 周りと壁の交点を１にする
		for( int i = 0; i < h * 2 + 1; i++ ){
			for( int j = 0; j < w * 2 + 1; j++ ){
				if( i == 0 || j == 0 ){
					maze[i][j] = 1;
				}else if( i % 2 == 0 && j % 2 == 0 ){
					maze[i][j] = 1;
				}else if( i == h * 2 || j == w * 2 ){
					maze[i][j] = 1;
				}
			}
		}
		// 壁の入力
		for( int i = 0; i < h * 2 - 1; i++ ){
			if( i % 2 == 0 ){
				for( int j = 0; j < (w + 1) / 2; j++ ){
					int wall;
					cin >> wall;
					maze[i + 1][(j + 1) * 2] = wall;
				}
			}else{
				for( int j = 0; j < w; j++ ){
					int wall;
					cin >> wall;
					maze[i + 1][(j * 2) + 1] = wall;
				}
			}
		}
		// 探索
		queue< P > que;
		que.push( P(1, 1) );
		maze[1][1] = 1;
		int ans = 0;
		while( !que.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = que.front().first + dx[i];
				int y = que.front().second + dy[i];
				if( maze[x][y] == 0 ){
					que.push( P( x, y ) );
					maze[x][y] = 1;
				}
			}
			//cout << que.front().first << ' ' << que.front().second << endl;
			ans++;
			//cout << ans << endl;
			if( que.front().first == h * 2 - 1 && que.front().second == w * 2 - 1 ){
				break;
			}
			que.pop();
		}
		cout << (ans + 1) / 2 << endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int h,w;
const int INF = 100000000;
typedef pair<int,int>P;
int dx[4]={2,0,-2,0},dy[4]={0,2,0,-2};
int main(){
    while(1){
        scanf("%d%d", &w, &h);
        if(h==0&&w==0) break;
        int field[h*2][w*2];
        int d[h][w];
        memset(field,0,sizeof(field));
        for(int i=0;i<h*2-1;i++){
            if(i%2==0)
                for(int j=0;j<w-1;j++)
                    scanf("%d",&field[i][j*2+1]);
            else
                for(int j=0;j<w;j++)
                    scanf("%d",&field[i][j*2]);
        }
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                d[i][j]=INF;
        queue<P> que;
        que.push(P(0,0));
        d[0][0]=0;
        while(que.size()){
            P p=que.front();que.pop();
            if(p.first==h*2-2&&p.second==w*2-2)break;
            for (int i=0;i<4;i++){
                int ny=p.first+dy[i],nx=p.second+dx[i];
                int my=p.first+(dy[i]/2),mx=p.second+(dx[i]/2);
                if(0<=ny&&ny<h*2-1&&0<=nx&&nx<w*2-1&&d[ny/2][nx/2]==INF&&field[my][mx]==0){
                    que.push(P(ny,nx));
                    d[ny/2][nx/2]=d[p.first/2][p.second/2]+1;
                }
            }
        }
        if(d[h-1][w-1]>=INF)
            printf("0\n");
        else
            printf("%d\n",(d[h-1][w-1])+1);
    }
    return 0;
}