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++: // 2013/06/28 Tazoe

#include <iostream>
using namespace std;

struct square{
	int w0;		// 上の壁
	int w1;		// 下の壁
	int w2;		// 左の壁
	int w3;		// 右の壁
};

int main()
{
	while(true){
		int w, h;
		cin >> w >> h;

		if(w==0&&h==0)
			break;

		struct square M[30][30];
		for(int y=0; y<h; y++){
			M[y][0].w2 = M[y][w-1].w3 = 1;
		}
		for(int x=0; x<w; x++){
			M[0][x].w0 = M[h-1][x].w1 = 1;
		}

		for(int i=0; i<2*h-1; i++){
			if(i%2==0){
				for(int j=0; j<w-1; j++){
					int b;
					cin >> b;

					M[i/2][j].w3 = M[i/2][j+1].w2 = b;
				}
			}
			else{
				for(int j=0; j<w; j++){
					int b;
					cin >> b;

					M[i/2][j].w1 = M[i/2+1][j].w0 = b;
				}
			}
		}
/*
		for(int y=0; y<h; y++){
			for(int x=0; x<w; x++){
				cout << M[y][x].w0*8+M[y][x].w1*4+M[y][x].w2*2+M[y][x].w3 << ' ';
			}
			cout << endl;
		}
*/
		int D[30][30];
		bool V[30][30];
		for(int y=0; y<h; y++){
			for(int x=0; x<w; x++){
				D[y][x] = 1000;
				V[y][x] = false;
			}
		}

		D[0][0] = 1;

		while(true){
			int D_min = 1000;
			int y_min = -1;
			int x_min = -1;

			for(int y=0; y<h; y++){
				for(int x=0; x<w; x++){
					if(V[y][x])
						continue;

					if(D[y][x]<D_min){
						D_min = D[y][x];
						y_min = y;
						x_min = x;
					}
				}
			}

			if(D_min==1000)
				break;

			V[y_min][x_min] = true;

			if(y_min==h-1 && x_min==w-1)
				break;

			int D1 = D[y_min][x_min]+1;
			if(M[y_min][x_min].w0==0 && D[y_min-1][x_min]>D1){
				D[y_min-1][x_min] = D1;
			}
			if(M[y_min][x_min].w1==0 && D[y_min+1][x_min]>D1){
				D[y_min+1][x_min] = D1;
			}
			if(M[y_min][x_min].w2==0 && D[y_min][x_min-1]>D1){
				D[y_min][x_min-1] = D1;
			}
			if(M[y_min][x_min].w3==0 && D[y_min][x_min+1]>D1){
				D[y_min][x_min+1] = D1;
			}
		}

		if(V[h-1][w-1]){
			cout << D[h-1][w-1] << endl;
		}
		else{
			cout << 0 << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int d[32][32];
int wx[32][32];
int wy[32][32];
typedef pair<int ,int> P;

int main(){
	int n,m,i,j,k;
	queue<P> que;

	while(cin >> n >> m, n){
		memset( d, 0, sizeof(d));
		while(que.size()) que.pop();

		for(i = 0; i <= m+1; i++) wy[i][0] = wy[i][n] = 1;
		for(i = 0; i <= n+1; i++) wx[0][i] = wx[m][i] = 1;
		for(i = 1;; i++){
			for(j = 1; j < n; j++)
				cin >> wy[i][j];
			if(i == m) break;
			for(j = 1; j < n+1; j++)
				cin >> wx[i][j];
		}

		que.push(P(1,1));
		d[1][1] = 1;

		while(que.size()){
			P p = que.front();
			que.pop();
			int px = p.first;
			int py = p.second;
			if(px == m && py == n) break;
			if(!wy[px][py] && !d[px][py+1]){
				d[px][py+1] = d[px][py] + 1;
				que.push(P(px,py+1));
			}
			if(!wx[px][py] && !d[px+1][py]){
				d[px+1][py] = d[px][py] + 1;
				que.push(P(px+1,py));
			}
			if(!wy[px][py-1] && !d[px][py-1]){
				d[px][py-1] = d[px][py] + 1;
				que.push(P(px,py-1));
			}
			if(!wx[px-1][py] && !d[px-1][py]){
				d[px-1][py] = d[px][py] + 1;
				que.push(P(px-1,py));
			}
		}
		
		cout << d[m][n] << endl;
	}
 return 0;
}