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<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long int i=0;i<n;++i)
typedef long long int ll;

int main(){

    while(1){
        int w,h;
        cin >> w >> h;
        if(w==0&&h==0){
            break;
        }
        vector<vector<int>> t(h,vector<int>(w-1));
        vector<vector<int>> y(h-1,vector<int>(w));
        rep(i,2*h-1){
            if(i%2==0){
                rep(j,w-1){
                    cin >> t[i/2][j];
                }
            }else{
                rep(j,w){
                    cin >> y[i/2][j];
                }
            }
        }
        queue<pair<int,int>> q;
        q.push({0,0});
        int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
        vector<vector<int>> f(h,vector<int>(w,-1));
        f[0][0]=1;
        while(q.size()!=0){
            pair<int,int> now=q.front();
            q.pop();
            rep(i,4){
                int nx=now.first+dx[i];
                int ny=now.second+dy[i];
                if(0<=nx&&nx<w&&0<=ny&&ny<h&&f[ny][nx]==-1){
                    //cout << now.first << " " << now.second <<" " << i << endl;
                    if(i==0&&y[now.second][now.first]==1)continue;
                    if(i==1&&t[now.second][now.first]==1)continue;
                    if(i==2&&y[now.second-1][now.first]==1)continue;
                    if(i==3&&t[now.second][now.first-1]==1)continue;
                    f[ny][nx]=f[now.second][now.first]+1;
                    q.push({nx,ny});
                }
            }
        }
        f[h-1][w-1]=max(f[h-1][w-1],0);
        cout << f[h-1][w-1] << 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));

		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;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

using T = tuple<int, int, int>;
const int INF = 1001001001;

bool r[33][33];
bool c[33][33];
int d[33][33];
int dx[4] = { 0, 0, -1, 1 }, dy[4] = { -1, 1, 0, 0 };

int main() {
    int w, h;
    while (cin >> w >> h && w) {
        for (int i = 0; i < 33; i++) {
            for (int j = 0; j < 33; j++) {
                r[i][j] = c[i][j] = true;
                d[i][j] = INF;
            }
        }
        for (int i = 0, k; i < 2 * h - 1; i++) {
            if (i % 2 == 0) {
                for (int j = 0; j < w - 1; j++) {
                    cin >> k;
                    if (k) c[i / 2][j] = false;
                }
            } else {
                for (int j = 0; j < w; j++) {
                    cin >> k;
                    if (k) r[i / 2][j] = false;
                }
            }
        }
        queue<T> que;
        que.push(T(0, 0, 1));
        d[0][0] = 1;
        while (que.size()) {
            T t = que.front(); que.pop();
            int cy = get<0>(t), cx = get<1>(t);
            for (int i = 0; i < 4; i++) {
                int ny = cy + dy[i], nx = cx + dx[i];
                if (!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
                if (dy[i] == -1 && !r[cy - 1][cx]) continue;
                if (dy[i] == 1 && !r[cy][cx]) continue;
                if (dx[i] == -1 && !c[cy][cx - 1]) continue;
                if (dx[i] == 1 && !c[cy][cx]) continue;
                if (d[ny][nx] == INF) {
                    d[ny][nx] = get<2>(t) + 1;
                    que.push(T(ny, nx, d[ny][nx]));
                }
            }
        }
        if (d[h - 1][w - 1] == INF) cout << 0 << endl;
        else cout << d[h - 1][w - 1] << endl;
    }
}