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++: /* competitive programming circle traP */
/* template */
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;

int h,w;
int vert[30][30];
int hori[30][30];
bool used[30][30];
int dy[4] = {1,0,-1,0};
int dx[4] = {0,1,0,-1};

bool cango(int sy,int sx,int ty,int tx){
  if(sy==ty && abs(sx-tx)==1){
    int x = min(sx,tx);
    return (vert[sy][x]==0);
  }else if(sx==tx && abs(sy-ty)==1){
    int y = min(sy,ty);
    return (hori[y][sx]==0);
  }
  return false;
}

int bfs(int sy,int sx,int gy,int gx){
  for(int i=0;i<h;i++)for(int j=0;j<w;j++){
    used[i][j] = false;
  }
  queue<int> qy,qx,qd;
  used[sy][sx] = true;
  qy.push(sy); qx.push(sx); qd.push(0);
  while(!qy.empty()){
    int y,x,d;
    y = qy.front(); qy.pop();
    x = qx.front(); qx.pop();
    d = qd.front(); qd.pop();
    if(y==gy && x==gx)return d;
    for(int i=0;i<4;i++){
      int ny = y+dy[i];
      int nx = x+dx[i];
      int nd = d+1;
      if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
      if(used[ny][nx])continue;
      if(!cango(y,x,ny,nx))continue;
      used[ny][nx] = true;
      qy.push(ny); qx.push(nx); qd.push(nd);
    }
  }
  return -1;
}

int main(){
  while(true){
    scanf("%d%d",&w,&h);
    if(h==0 && w==0)break;
    for(int k=0;k<2*h-1;k++){
      int i = k/2;
      if(k%2==0){
        // vert
        for(int j=0;j<w-1;j++){
          scanf("%d",&vert[i][j]);
        }
      }else{
        // hori
        for(int j=0;j<w;j++){
          scanf("%d",&hori[i][j]);
        }
      }
    }
    int ans = bfs(0,0,h-1,w-1)+1;
    printf("%d\n",ans);
  }
  return 0;
}

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