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<cstdio>
#include<string>
#include<queue>

using namespace std;
int bfs(int x,int y);
int w,h;
int i,j;
int dy[4] = {1,0,-1,0}, dx[4] = {0,-1,0,1};
string map[100];
int dis[100][100];
queue<pair<int,int> > q;
int main(){
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    w = 2*w -1;
    h = 2*h -1;
    cin.ignore();
    for(i = 0;i < h;i++){
      getline(cin,map[i]);
      if(i%2 == 0)
	map[i][w-1] = ' ';
    }
    bfs(0,0);
    //cout << dis[4][2] +1<<endl;
    if(dis[h-1][w-1] > 0){
    cout << dis[h-1][w-1] +1<<endl;
    }else{
      cout << "0" <<endl;
    }
  }
  return 0;
}

int bfs(int x,int y){
  int k;
  //cout << "x = "<<x<<endl;
  //cout << "y = "<<y<<endl;
  pair<int,int> tmp;
  for(k = 0;k < 4;k++){
    if(y +  2*dy[k] >-1 && y + 2*dy[k] < h && x + 2*dx[k] < w && x + 2*dx[k] > -1){
      // cout << "dx = "<<dx[k]<<endl;
      //cout << "dy = "<<dy[k]<<endl;
      if(map[y + dy[k]][x + dx[k]] == '0'){
	map[y + dy[k]][x + dx[k]] ='1';
	tmp.second = x + 2*dx[k];
	tmp.first = y +2*dy[k];
	q.push(tmp);
	dis[y + 2*dy[k]][x + 2*dx[k]] = dis[y][x] + 1;
	//cout << dis[y + 2*dy[k]][x + 2*dx[k]]<<endl;
      }
    }
  }
  while(q.size() != 0){
    tmp = q.front();
    q.pop();
    bfs(tmp.second,tmp.first);
  }
  return 0;
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

int f[60][60];
int w, h;
int gx, gy;

int solve() {

  int memo[h][w];
  rep (i, h) rep (j, w) memo[i][j] = 0;

  queue<pair<int, pair<int, int>>> q;
  q.push(cons(1, cons(0, 0)));

  while (!q.empty())
  {
    int d = car(q.front());
    int x = cadr(q.front());
    int y = cddr(q.front());
    q.pop();

    assert(x % 2 == 0);
    assert(y % 2 == 0);

    if (memo[x/2][y/2]) continue;
    memo[x/2][y/2] = 1;

    //cerr << cons(x, y) << endl;
    if (x == gx && y == gy) return d;

    if (x > 0 && f[x-1][y] == 0)
      q.push(cons(d + 1, cons(x - 2, y)));
    if (y > 0 && f[x][y-1] == 0)
      q.push(cons(d + 1, cons(x, y - 2)));
    if (x < gx - 1 && f[x+1][y] == 0)
      q.push(cons(d + 1, cons(x + 2, y)));
    if (y < gy - 1 && f[x][y+1] == 0)
      q.push(cons(d + 1, cons(x, y + 2)));
  }
  cerr << "nothing" << endl;
  return 0;
}

int main() {

more:;

  cin >> w >> h;

  if (!cin || !w && !h) return 0;

  gx = h + h - 2;
  gy = w + w - 2;

  rep (i, h + h - 1) {
    rep (j, w + w - 1) {
      int d = 0;
      if ((i + j) % 2) {
        cin >> d;
      }
      f[i][j] = d;
    }
  }

  cout << solve() << endl;

  goto more;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> P;
#define MAX (1000)
#define MAX_WH (61)
#define mp make_pair

int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};

// dfs では解けない?

int main(){
	int w, h;
	while( cin >> w >> h, w || h ){
		// input
		int H = 2*h+1, W = 2*w+1;
		vector<vi> maze(MAX_WH, vi(MAX_WH));
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				if( i == 0 || i == H-1 || j == 0 || j == W-1 ){
					maze[i][j] = -1;
					continue;
				}
				if( (i%2 == 1 && j%2 == 0) || (i%2 == 0 && j%2 == 1) ){
					int wall;
					cin >> wall;
					maze[i][j] = -wall;
				}else if( i%2 == 0 && j%2 == 0 ){
					maze[i][j] = -1;
				}else{
					maze[i][j] = 0;
				}
			}
		}

		int gl_w = W-2, gl_h = H-2;
		maze[1][1] = 1;
		queue<P> que;
		que.push(mp(1,1));
		while( !que.empty() ){
			int x = que.front().first, y = que.front().second;
			que.pop();
			for( int i = 0; i < 4; i++ ){
				int nx = x + dx[i], ny = y + dy[i];
				if( nx <= 0 || W-1 <= nx || ny <= 0 || H-1 <= ny ) continue;
				if( maze[ny][nx] == 0 ){
					maze[ny][nx] = maze[y][x]+1;
					que.push(mp(nx,ny));
				}
			}
		}

		// output
		int ans = maze[gl_h][gl_w];
		if( ans == 0 ) cout << 0 << endl;
		else cout << (ans+1)/2 << endl;
	}
}