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 <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
typedef long long ll;

#define INF 1000000000

#define U 1
#define D 2
#define R 4
#define L 8

typedef pair<pair<int, int>, int> P;

int mat[30][30];
bool visited[30][30];

int main(){
  int w,h;
  while(cin>>w>>h, w|h){
    rep(i,h)rep(j,w){ mat[i][j]=0; visited[i][j]=false; }

    // input
    rep(i,h){
      rep(j,w-1){
        int d;
        scanf("%d", &d);
        if(d==0){
          mat[i][j] |= R;
          mat[i][j+1] |= L;
        }
      }

      if(i==h-1) break;

      rep(j,w){
        int d;
        scanf("%d", &d);
        if(d==0){
          mat[i][j] |= D;
          mat[i+1][j] |= U;
        }
      }
    }

    // wfs
    queue<P> q;
    q.push(mp(mp(0,0),1));
    visited[0][0]=true;
    int res=0;
    while(!q.empty()){
      P p = q.front(); q.pop();
      int x=p.first.first, y=p.first.second;
      if(x==h-1 && y==w-1){
        res = p.second;
        break;
      }
      const int d[] = {U, D, R, L};
      const int dy[]= {0,0,1,-1};
      const int dx[]= {-1,1,0,0};
      rep(i,4){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0 || nx>=h || ny<0 || ny>=w || visited[nx][ny]) continue;
        if((mat[x][y]&d[i])==0) continue;

        visited[nx][ny]=true;
        q.push(mp(mp(nx,ny), p.second+1));
      }
    }

    cout<<res<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)

int hyou[40][40];
int hyou2[40][40];
int W,H;
int cnt;


int main() {
	while(cin >> W >> H , W) {
		rep(i, 35) {
			rep(j, 35){
				hyou[i][j] = 0;
				hyou2[i][j] = 0;
			}
		}
		int c;
		rep(i, H * 2 - 1) {
			int h = i / 2 + 1;
			if (i % 2 == 0) {
				rep(w, W - 1) {
					cin >> c;
					if (c == 0) {
						hyou[h][w + 1] += 1;
					}
				}
			} else {
				rep(w, W) {
					cin >> c;
					if (c == 0) {
						hyou[h][w + 1] += 2;
					}
				}
			}
		}
		
		int Q[100000][2];
		int p = 0;
		int t = 1;
		Q[0][0] = 1;
		Q[0][1] = 1;
		hyou2[1][1] = 1;
		while (t > p) {
			int x = Q[p][0];
			int y = Q[p][1];
			int cnt = hyou2[y][x];
			p++;
			if (hyou[y][x] & 1) {
				if(hyou2[y][x + 1] == 0) {
					hyou2[y][x + 1] = cnt + 1;
					Q[t][0] = x + 1;
					Q[t][1] = y;
					t++;
				}
			}
			
			if (hyou[y][x - 1] & 1) {
				if(hyou2[y][x - 1] == 0) {
					hyou2[y][x - 1] = cnt + 1;
					Q[t][0] = x - 1;
					Q[t][1] = y;
					t++;
				}
			}
			
			if (hyou[y][x] & 2) {
				if(hyou2[y + 1][x] == 0) {
					hyou2[y + 1][x] = cnt + 1;
					Q[t][0] = x;
					Q[t][1] = y + 1;
					t++;
				}
			}
			if (hyou[y - 1][x] & 2) {
				if(hyou2[y - 1][x] == 0) {
					hyou2[y - 1][x] = cnt + 1;
					Q[t][0] = x;
					Q[t][1] = y - 1;
					t++;
				}
			}
		}
		cout << hyou2[H][W] << endl;
	}
}