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 <algorithm>
#include <numeric>
#include <map>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define REP(i,n) for(int i=0;i<n;i++)


int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int INF=1<<28;
int main() {
	while (true) {
		int W,H;cin >> W >> H;if(W==0 && H==0)break;

		vector<vector<vector<bool>>> wall(H,vector<vector<bool>>(W,vector<bool>(4)));

		REP(inpy,2*H-1){
			int y=inpy/2;
			if(inpy%2==0){
				REP(x,W-1){
					int b;cin >> b;
					wall[y][x][0]=b;
					if(x-1>=0)wall[y][x-1][2]=b;
				}
			}else{
				REP(x,W){
					int b;cin >>b;
					wall[y][x][1]=b;
					if(x+1<W)wall[y][x+1][3]=b;
				}
			}
		}

		queue<pair<int,pair<int,int>>> que;
		que.push(make_pair(0,make_pair(0,0)));
		vector<vector<int>> ds(H,vector<int>(W,INF));ds[0][0]=0;

		while(!que.empty()){
			pair<int,pair<int,int>> p=que.front();que.pop();
			int y=p.second.first,x=p.second.second;
			REP(d,4)if(!wall[y][x][d]){	
				
				int ny=y+dy[d],nx=x+dx[d];			
				if(0<=nx && nx < W && 0 <= ny && ny < H){
					if(ds[ny][nx]>ds[y][x]+1){
						que.push(make_pair(p.first+1,make_pair(ny,nx)));
						ds[ny][nx]=ds[y][x]+1;
					}
				}
			}
		}
		if(ds[H-1][W-1]<INF)cout << ds[H-1][W-1]+1<<endl;
		else cout << 0 <<endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

class State{
public:
  int x, y, cost;

  State(int x, int y, int cost) :
    x(x), y(y), cost(cost) {}
};

int w, h;
char t[102][102];
bool closed[102][102];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void solve(){
  queue<State> open;
  open.push(State(0, 0, 1));
  memset(closed, 0, sizeof(closed));
  closed[0][0] = true;

  while(!open.empty()){
    State st = open.front();
    open.pop();

    if(st.x == w - 1 && st.y == h - 1){
      cout << st.cost << endl;
      return;
    }

    for(int i = 0; i < 4; i++){
      int nx1 = st.x + dx[i];
      int ny1 = st.y + dy[i];
      int nx2 = st.x + 2 * dx[i];
      int ny2 = st.y + 2 * dy[i];

      if(nx2 < 0 || w <= nx2 || ny2 < 0 || h <= ny2) continue;
      if(t[ny1][nx1] != '0' || t[ny2][nx2] != '0') continue;
      if(closed[ny2][nx2]) continue;

      closed[ny2][nx2] = true;
      open.push(State(nx2, ny2, st.cost + 1));
    }
  }

  cout << 0 << endl;
}

int main(){
  while(cin >> w >> h, w || h){
    char buff[102];
    fgets(buff, 102, stdin);

    h = 2 * h - 1;
    w = 2 * w - 1;

    for(int i = 0; i < h; i++){
      fgets(t[i], 102, stdin);
    }

    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(t[i][j] == '0' || t[i][j] == '1') continue;
        t[i][j] = '0';
      }
    }

    solve();
  }
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int> P;
const int MAX_W = 30*2;
const int MAX_H = MAX_W;
int W, H;
bool G[MAX_H][MAX_W];

void init() {
  fill(G[0], G[MAX_W], 0);
  for(int i = 1; i < H; i += 2) {
    for(int j = 1; j < W; j += 2) {
      G[i][j] = 1;
    }
  }
}

int solve() {
  queue<P> que;
  int vis[MAX_H][MAX_W];
  fill(vis[0], vis[MAX_H], -1);
  vis[0][0] = 2;
  que.push(P(0,0));
  while(!que.empty()) {
    P p = que.front();
    que.pop();
    for(int i = -1; i <= 1; ++i) {
      for(int j = -1; j <= 1; ++j) {
	if(i*j) continue;
	int nx = p.first + j;
	int ny = p.second + i;
	if(nx < 0 || nx >= W) continue;
	if(ny < 0 || ny >= H) continue;
	if(vis[ny][nx] != -1) continue;
	if(G[ny][nx]) continue;
	vis[ny][nx] = vis[p.second][p.first] + 1;
	if(nx == W-1 && ny == H-1) return vis[ny][nx];
	que.push(P(nx,ny));
      }
    }
  }
  return 0;
}

int main() {
  while(cin >> W >> H && (W | H)) {
    W = W*2-1;
    H = H*2-1;
    init();
    for(int i = 0; i < H; ++i) {
      for(int j = 1-i%2; j < W; j += 2) {
	cin >> G[i][j];
      }
    }
    cout << solve()/2 << endl;
  }
  return 0;
}