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++)
#define ALL(a) a.begin(),a.end()
#define EACH(it,a) for(auto it=a.begin();it!=a.end();it++)

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


int INF=1<<28;

namespace ShortestPath{
    struct Edge{
        int from,to,cost;
        Edge(int from,int to,int cost)
            : from(from),to(to),cost(cost) {};
    };
    ostream& operator <<(ostream& os,const Edge& e){
            os<<"("<<e.from<<"->"<<e.to<<")";
            return os;
    }
    struct Task{
        int prev,pos,cost;
        Task(int prev,int pos,int cost)
            :prev(prev),pos(pos),cost(cost){};
        bool operator>(const Task& r) const{
            return cost>r.cost;
        }
    };
    typedef vector<vector<Edge> > Graph;
    typedef  vector<vector<int> > Mat;

    //verified by codoforces 144D http://codeforces.com/contest/144/submission/4976825
    // // 負の辺がない
    // // O(E*logV)
    vector<int> dijkstra(const Graph& g,const int s,vector<int>& prev){
        const int V=g.size();
        vector<int> d(V,INF);d[s]=0;
        fill(ALL(prev), -2);
        priority_queue<Task,vector<Task>,greater<Task> > que;
        que.push(Task(-1,s,0));
        while(!que.empty()){
            Task task=que.top();que.pop();
            //if(prev[task.pos]!=-2)continue;
            if(d[task.pos]<task.cost)continue;
            prev[task.pos]=task.prev;
            EACH(e,g[task.pos]){
                if(d[e->to]>d[e->from]+e->cost){
                    d[e->to]=d[e->from]+e->cost;
                    que.push(Task(e->from,e->to,d[e->to]));
                }
            }   
        }
        return d;
    }
    vector<int> dijkstra(const Graph& g,const int s){
        vector<int> prev(g.size());return dijkstra(g,s,prev);
    }
}
using namespace ShortestPath;


int W,H;
int enc(int y,int x){
	return y*W+x;
}


int main() {


	while (true) {
		cin >> W >> H;if(W==0 && H==0)break;
        

        Graph g(W*H);
        
		REP(inpy,2*H-1){
			int y=inpy/2;
			if(inpy%2==0){
				REP(x,W-1){
					int b;cin >> b;
					if(b==0){
						g[enc(y,x)].push_back(Edge(enc(y,x),enc(y,x+1),1));
						g[enc(y,x+1)].push_back(Edge(enc(y,x+1),enc(y,x),1));
					}
				}
			}else{
				REP(x,W){
					int b;cin >> b;
					if(b==0){
						g[enc(y,x)].push_back(Edge(enc(y,x),enc(y+1,x),1));
						g[enc(y+1,x)].push_back(Edge(enc(y+1,x),enc(y,x),1));
					}
				}
			}
		}
        int d=dijkstra(g,enc(0,0))[enc(H-1,W-1)];
		if(d<INF)cout << d+1<<endl;
		else cout << 0 <<endl;
	}
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };
int main(){
	int w, h;
	while( cin >> w >> h && w && h ){
		int maze[65][65];
		// 周りと壁の交点を１にする
		for( int i = 0; i < h * 2 + 1; i++ ){
			for( int j = 0; j < w * 2 + 1; j++ ){
				if( i == 0 || j == 0 ){
					maze[i][j] = 1;
				}else if( i % 2 == 0 && j % 2 == 0 ){
					maze[i][j] = 1;
				}else if( i == h * 2 || j == w * 2 ){
					maze[i][j] = 1;
				}else if( i % 2 == 1 && j % 2 == 1 ){
					maze[i][j] = 0;
				}else{
					int wall;
					cin >> wall;
					maze[i][j] = wall;
				}
			}
		}
		// 壁の入力
		/*for( int i = 0; i < h * 2 - 1; i++ ){
			if( i % 2 == 0 ){
				for( int j = 0; j < (w + 1) / 2; j++ ){
					int wall;
					cin >> wall;
					maze[i + 1][(j + 1) * 2] = wall;
				}
			}else{
				for( int j = 0; j < w; j++ ){
					int wall;
					cin >> wall;
				}
			}
		}*/
		// 探索
		queue< P > que;
		que.push( P(1, 1) );
		maze[1][1] = 1;
		while( !que.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = que.front().first + dx[i];
				int y = que.front().second + dy[i];
				if( maze[x][y] == 0 ){
					que.push( P( x, y ) );
					maze[x][y] = maze[que.front().first][que.front().second] + 1;
				}
			}
			if( que.front().first == h * 2 - 1 && que.front().second == w * 2 - 1 ){
				break;
			}
			que.pop();
		}
		/*
		for( int i = 0; i < h * 2 + 1; i++ ){
			for( int j = 0; j < w * 2 + 1; j++ ){
				cout << maze[i][j] << ' ';
			}
			cout << endl;
		}*/
		cout << (maze[h * 2 - 1][w * 2 -1] + 1) / 2 << endl;
	}
}