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 <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 = 1;
		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 / 2 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int w,h;
typedef pair<int, int> P;
vector<vector<int>> maze;
vector<vector<int>> thismap;
vector<int> dx = {1, 0, -1, 0};
vector <int> dy = {0, 1, 0, -1};

bool check(int x, int y, int x2, int y2){
    if (x2<0 || x2>=w || y2<0 || y2>=h) return false;
    if (x==x2){
        if (thismap[y+y2][x]!=0) return false;
    } else if (y==y2){
        if (thismap[y*2][(x+x2)/2]!=0) return false;
    }
    return true;
}

int bfs(){
    queue<P> que;
    que.push(P(0,0));
    maze[0][0]=1;
    
    while (que.size()){
        P p = que.front(); que.pop();
        if (p.first==w-1 and p.second==h-1) break;
        for (int i=0; i<4; i++){
            int nx = p.first + dx[i], ny = p.second + dy[i];
            
            if (check(p.first, p.second, nx, ny) && maze[nx][ny]==0){
                que.push(P(nx,ny));
                maze[nx][ny] = maze[p.first][p.second] + 1;
            }
        }
    }
    return maze[w-1][h-1];
}

int main(){
    cin >> w >> h;
    while (w!=0 || h!=0){
        vector<vector<int>> map2(h*2-1, vector<int>(w));
        for (int i=0; i<h*2-1; i++){
            if (i%2==0){
                for (int j=0; j<w-1; j++){
                    cin >> map2[i][j];
                }
            }else{
                for (int j=0; j<w; j++){
                    cin >> map2[i][j];
                }
            }
        }
        vector<vector<int>> maze2(w, vector<int>(h));
      
        thismap = map2;
        maze = maze2;
        int ans = bfs();
        cout << ans << endl;
        
        cin >> w >> h;
    }
}

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

int W,H,w[33][33],h[33][33];

struct point{
  int x,y;
};

int bfs(){
  point u,v;
  queue<point> Q;
  int D[37][37],ny,nx;
  int dy[4] = {0,-1,0,1};
  int dx[4] = {1,0,-1,0};

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++) D[i][j] = 10000;
  }

  u.y = u.x = 0;
  D[u.y][u.x] = 1;
  Q.push(u);

  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    if(u.y == H-1 && u.x == W-1) return D[u.y][u.x];

    for(int r=0; r<4; r++){
      nx = u.x + dx[r];
      ny = u.y + dy[r];
      if(nx < 0 || ny < 0 || nx >= W || ny >= H)  continue;
      if(D[ny][nx] != 10000) continue;
      if(r == 0 && w[u.y][u.x] == 1) continue;
      if(r == 1 && h[ny][u.x] == 1) continue;
      if(r == 2 && w[u.y][nx] == 1) continue;
      if(r == 3 && h[u.y][u.x] == 1) continue;
      D[ny][nx] = D[u.y][u.x] + 1;
      v.x = nx;
      v.y = ny;
      Q.push(v); 
    }
  }
  return 0;
}    

int main(){
  int i,j;
  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0) break;

    for(i=0;i<W-1;i++){
      cin >> w[0][i];
    }

    for(i=1;i<H;i++){
      for(j=0;j<W;j++){
	cin >> h[i-1][j];
      }
      for(j=0;j<W-1;j++){
	cin >> w[i][j];
      }
    }
    cout << bfs() << endl;

  }

  return 0;
}