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;

class Edge{
	public:
		long long cost;
		int from, to;
		Edge(long long c, int f, int t){
			cost = c;
			from = f;
			to = t;
		}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
bool operator < (const Edge &e, const Edge &f){
	return e.cost != f.cost ? e.cost > f.cost : e.from != f.from ? e.from < f.from : e.to < f.to;
}
constexpr long long INF = 1e18+9;
void dijkstra(int s, const Graph &g, vector<long long> &dist){
	int V = g.size();
	dist = vector<long long>(V, INF);
	dist[s] = 0;
	priority_queue<Edge> q;
	q.push(Edge(0, s, s));
	while(!q.empty()){
		Edge e = q.top();
		q.pop();
		if(dist[e.to] < e.cost) continue;
		for(int i=0; i<(int)g[e.to].size(); ++i){
			Edge f = g[e.to][i];
			if(dist[f.to] > dist[e.to] + f.cost){
				dist[f.to] = dist[e.to] + f.cost;
				q.push(Edge(dist[f.to], s, f.to));
			}
		}
	}
}

void solve(int w, int h){
	Graph g(w*h);
	for(int i=0; i<2*h-1; ++i){
		if(i % 2 == 0){
			for(int j=0; j<w-1; ++j){
				int t;
				cin >> t;
				if(t == 0){
					g[w*(i/2)+j].push_back(Edge(1, w*(i/2)+j, w*(i/2)+j+1));
					g[w*(i/2)+j+1].push_back(Edge(1, w*(i/2)+j+1, w*(i/2)+j));
				}
			}
		}else{
			for(int j=0; j<w; ++j){
				int t;
				cin >> t;
				if(t == 0){
					g[w*(i/2)+j].push_back(Edge(1, w*(i/2)+j, w*(i/2+1)+j));
					g[w*(i/2+1)+j].push_back(Edge(1, w*(i/2+1)+j, w*(i/2)+j));
				}
			}
		}
	}
/*	for(int i=0; i<w*h; ++i){
		for(int j=0; j<g[i].size(); ++j){
			cout << g[i][j].to << " ";
		}
		cout << "\n";
	}
*/	vector<long long> v;
	dijkstra(0, g, v);
	if(v[w*h-1] == INF) cout << 0 << "\n";
	else cout << v[w*h-1]+1 << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int w, h;
		cin >> w >> h;
		if(w+h==0) return 0;
		solve(w, h);
	}
}

C++: #include "bits/stdc++.h"
#define int long long
#define REP(i, s, n) for (signed i = (s); i < signed(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()


const int INF = 1 << 29;

using namespace std;

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

int dr[] = { -1, 0, 1, 0 };
int dc[] = { 0, -1, 0, 1 };
const int MOD = 1000000007;

signed main() {

	int w, h;
	while (cin >> w >> h && w)
	{
		vector<VV> stage(h, VV(w, V(4)));
		rep(i, h) {
			stage[i][w - 1][3] = stage[i][0][1] = 1;
		}
		rep(j, w) {
			stage[h - 1][j][2] = stage[0][j][0] = 1;
		}
		rep(i, h * 2 - 1) rep(j, w - (i % 2 == 0)) {
			int b; cin >> b;
			if (i % 2 == 0) {
				stage[i / 2][j][3] = stage[i / 2][j + 1][1] = b;
			}
			else {
				stage[i / 2][j][2] = stage[i / 2 + 1][j][0] = b;
			}
		}

		queue<IP> que;
		vector<vector<bool>> memo(h, vector<bool>(w));
		que.push(IP(1, P(0, 0)));
		memo[0][0] = true;
		int ans = 0;
		while (!que.empty() && ans == 0) {
			IP ip = que.front(); que.pop();
			int step = ip.first;
			int pr = ip.second.first, pc = ip.second.second;

			rep(i, 4) if (stage[pr][pc][i] == 0) {
				int nr = pr + dr[i], nc = pc + dc[i];
				if (nr == h - 1 && nc == w - 1) {
					ans = step + 1;
					break;
				}
				if (!memo[nr][nc]) {
					que.push(IP(step + 1, P(nr, nc)));
					memo[nr][nc] = true;
				}
			}
		}
		cout << ans << endl;
	}

}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <utility>

using namespace std;
typedef pair<int, int> P;

int main()
{
	int W, H;
	while (cin >> W >> H, W || H) {
		vector< vector< vector<int> > > kabe(4, vector< vector<int> >(W, vector<int>(H, 1) ) );
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W - 1; j++) {
				cin >> kabe[1][j][i];
				kabe[3][j + 1][i] = kabe[1][j][i];
			}
			if (i < H - 1) {
				for (int j = 0; j < W; j++) {
					cin >> kabe[2][j][i];
					kabe[0][j][i + 1] = kabe[2][j][i];
				}
			}
		}
		vector< vector<int> > maze(W, vector<int>(H, -1));
		queue<P> que;
		maze[0][0] = 1;
		que.push(P(0, 0));
		while (!que.empty()) {
			P temp;
			temp = que.front();
			int f = temp.first, s = temp.second;
			que.pop();
			if (!kabe[0][f][s] && maze[f][s - 1] == -1) {
				maze[f][s - 1] = maze[f][s] + 1;
				que.push(P(f, s - 1));
			}
			if (!kabe[1][f][s] && maze[f + 1][s] == -1) {
				maze[f + 1][s] = maze[f][s] + 1;
				que.push(P(f + 1, s));
			}
			if (!kabe[2][f][s] && maze[f][s + 1] == -1) {
				maze[f][s + 1] = maze[f][s] + 1;
				que.push(P(f, s + 1));
			}
			if (!kabe[3][f][s] && maze[f - 1][s] == -1) {
				maze[f - 1][s] = maze[f][s] + 1;
				que.push(P(f - 1, s));
			}
		}
		if (maze[W - 1][H - 1] == -1)
			cout << 0 << endl;
		else
			cout << maze[W - 1][H - 1] << endl;
	}
	return 0;
}