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 <vector>
#include <queue>
#include <algorithm>
using namespace std;

void push_d(queue<vector<int> > &q, int x, int y, int l) {
    vector<int> d;
    d.push_back(x);
    d.push_back(y);
    d.push_back(l);
    q.push(d);
}

int main() {
    while (1) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;

        vector<vector<bool> > bw, bh;
        bool c;
        for (int i = 0; i < h; i++) {
            vector<bool> _bh;
            bh.push_back(_bh);
            for (int x = 0; x < w - 1; x++) {
                cin >> c;
                bh[i].push_back(c);
            }
            if (i < h - 1) {
                vector<bool> _bw;
                bw.push_back(_bw);
                for (int y = 0; y < w; y++) {
                    cin >> c;
                    bw[i].push_back(c);
                }
            }
        }

        queue<vector<int> > q;
        vector<int> goal;
        push_d(q, 0, 0, 0);
        while (!q.empty()) {
            vector<int> d = q.front();
            q.pop();
            int x = d[0], y = d[1], l = d[2] + 1;
            if (x == w - 1 && y == h - 1) {
                goal.push_back(l);
                continue;
            }
            if (0 <= x && x < w && 0 <= y && y < h) {
                // down
                if (y < h-1 && !bw[y][x]) {
                    bw[y][x] = true;
                    push_d(q, x, y+1, l);
                }
                // up
                if (0 <= y-1 && y-1 < h-1 && !bw[y-1][x]) {
                    bw[y-1][x] = true;
                    push_d(q, x, y-1, l);
                }
                // right
                if (x < w-1 && !bh[y][x]) {
                    bh[y][x] = true;
                    push_d(q, x+1, y, l);
                }
                // left
                if (0 <= x-1 && x-1 < w-1 && !bh[y][x-1]) {
                    bh[y][x-1] = true;
                    push_d(q, x-1, y, l);
                }
            }
        }

        int route = 0;
        if (goal.size() > 0) {
            route = *min_element(goal.begin(), goal.end());
        }
        cout << route << endl;
    }
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	int w,h;
	while(cin>>w>>h,!(w==0&&h==0)){
		
		int a[30][30][4]={}; //0left,1down,2right,3up
		REP(i,30){REP(j,30){REP(k,4){a[i][j][k]=1;}}}
		
		int tmp;
		for(int i=0;i<h;i++){
			for(int j=0;j<w-1;j++){
				if(cin>>tmp && tmp==0){
					a[j][i][2]=0;a[j+1][i][0]=0;
				}
			}
			if(i!=h-1){
			for(int j=0;j<w;j++){
				if(cin>>tmp && tmp==0){
					a[j][i][1]=0;a[j][i+1][3]=0;
				}
			}}
		}
		
		int ans[30][30]={}; //答えの配列
		int b[901][900][2]={};//n回目の探索に使うx,yのセット
		int t[901]={};//n回目の探索数
		
		ans[0][0]=1;
		b[0][0][0]=0;
		b[0][0][1]=0;
		t[0]=1;
		
		for(int i=0;i<900;i++)//i回目の探索
		{
			for(int j=0;j<t[i];j++)  //x=b[i][j][0] y=b[i][j][1]
			{
				int x=b[i][j][0];
				int y=b[i][j][1];
				//left
				if(x-1>-1 && a[x][y][0]==0 && ans[x-1][y]==0){
					ans[x-1][y]=ans[x][y]+1;
					b[i+1][t[i+1]][0]=x-1;
					b[i+1][t[i+1]][1]=y;
					t[i+1]++;
				}
				//right
				if(x+1<w && a[x][y][2]==0 && ans[x+1][y]==0){
					ans[x+1][y]=ans[x][y]+1;
					b[i+1][t[i+1]][0]=x+1;
					b[i+1][t[i+1]][1]=y;
					t[i+1]++;
				}
				//down
				if(y+1<h && a[x][y][1]==0 && ans[x][y+1]==0){
					ans[x][y+1]=ans[x][y]+1;
					b[i+1][t[i+1]][0]=x;
					b[i+1][t[i+1]][1]=y+1;
					t[i+1]++;
				}
				//up
				if(y-1>-1 && a[x][y][3]==0 && ans[x][y-1]==0){
					ans[x][y-1]=ans[x][y]+1;
					b[i+1][t[i+1]][0]=x;
					b[i+1][t[i+1]][1]=y-1;
					t[i+1]++;
				}
			}
			if(t[i+1]==0){break;}
		}
		cout << ans[w-1][h-1]<< endl;	
	}
	return 0;
}