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

int visit(int h, int w,vector<vector<list<pair<int,int> > > > adj){
    int i, j;
    vector<int> int_sub;
    for(j = 0; j <= 30; j++){
        int_sub.push_back(0);
    }
    vector<vector<int> > counta;
    for(i = 0; i <= 30; i++){
        counta.push_back(int_sub);
    }
    counta[1][1] = 1;
    queue<pair<int,int> > que;
    que.push(make_pair(1,1));
    pair<int,int> p;
    list<pair<int,int> >::iterator t;
    while(!que.empty()){
        p = que.front();
        que.pop();
        for(t = adj[p.first][p.second].begin(); t != adj[p.first][p.second].end(); ++t){
            if(counta[(*t).first][(*t).second] == 0){
                counta[(*t).first][(*t).second] = counta[p.first][p.second] + 1;
                que.push(make_pair(((*t).first),(*t).second));
            }
        }
    }
    i = counta[h][w];
    return i;
}

int main(){
    int i,j;
    int h, w;
    bool wall;
    vector<bool> bool_sub;
    for(j = 0; j <= 30; j++){
        bool_sub.push_back(0);
    }
    vector<vector<bool> > wall_v, wall_p;
    for(i = 0; i <= 30; i++){
        wall_v.push_back(bool_sub);
        wall_p.push_back(bool_sub);
    }
    list<pair<int,int> > intint_subsub;
    vector<list<pair<int,int> > > intint_sub;
    for(j = 0; j <= 30; j++){
        intint_sub.push_back(intint_subsub);
    }
    vector<vector<list<pair<int,int> > > > adj;
    for(i = 0; i <= 30; i++){
        adj.push_back(intint_sub);
    }
    while(1){
        cin >> w >> h;
        if(h == 0 && w == 0) break;
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w-1; j++){
                cin >> wall;
                wall_v[i][j] = wall;
            }
            for(j = 1; j <= w; j++){
                cin >> wall;
                wall_p[i][j] = wall;
            }
        }
        for(j = 1; j <= w-1; j++){
            cin >> wall;
            wall_v[h][j] = wall;
        }
        for(i = 0; i <= 30; i++){
            for(j = 0; j <= 30; j++){
                adj[i][j] = intint_subsub;
            }
        }
        for(i = 1; i <= h; i++){
            for(j = 1; j <= w-1; j++){
                if(!wall_v[i][j]){
                    adj[i][j].push_back(make_pair(i,j+1));
                    adj[i][j+1].push_back(make_pair(i,j));
                }
            }
        }
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w; j++){
                if(!wall_p[i][j]){
                    adj[i][j].push_back(make_pair(i+1,j));
                    adj[i+1][j].push_back(make_pair(i,j));
                }
            }
        }
        cout << visit(h,w,adj) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
queue< pair<int,int> > q;
int grid[100][100];
int W, H, _W, _H;

void debug() {
  cout << endl << "-----" << endl;
  for (int i = 0; i < _H; i++) {
    for (int j = 0; j < _W; j++) {
      cout << grid[j][i] << ",";
    }
    cout<<endl;
  }
  cout << "-----" << endl;
}

void bfs() {
  int mx[4] = { 1, 0, -1, 0 };
  int my[4] = { 0, 1, 0, -1 };
  q.push(make_pair(0,0));
  grid[0][0] = 1;
  while (q.size() > 0) {
    pair<int,int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int _x = x+mx[i], _y = y+my[i];
      if (_x>=_W || _y>=_H || _x<0 || _y<0 || grid[_x][_y] != INF) continue;
      grid[_x][_y] = grid[x][y]+1;
      q.push(make_pair(_x,_y));
    }
  }
}

signed main() {
  while(cin>>W>>H, W+H!=0) {
    _W = W*2-1; _H = H*2-1;
    for (int i = 0; i < _H; i++) {
      for (int j = 0; j < _W; j++) {
        grid[j][i] = INF;
      }
   }
    for (int i = 0; i < _H; i++) {
      int t;
      if (i%2==0) {
        for (int j = 0; j < W-1; j++) {
          cin>>t; if(t==0)continue;
          grid[j*2+1][max(i-1, 0LL)] = -1;
          grid[j*2+1][i] = -1;
          grid[j*2+1][min(i+1, _H-1)] = -1;
          // 0 -> 1
          // 1 -> 3
          // 2 -> 5
          // 3 -> 7
        }
      } else {
        for (int j = 0; j < W; j++) {
          cin>>t; if(t==0)continue;
          grid[max(j*2-1, 0LL)][i] = -1;
          grid[j*2][i] = -1;
          grid[min(j*2+1,_W-1)][i] = -1;
          // 0 -> 0
          // 1 -> 2
          // 2 -> 4
          // 3 -> 6   
        }
      }
    }
    //debug();
    bfs();
    //debug();
    int ans = grid[_W-1][_H-1];
    if (ans == INF) {
      ans = 0;
    } else {
      ans = ans/2+1;
    }
    cout << ans << endl;
  }
}