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 <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;



int a[50][50][50][50]={0};
int vis[50][50];
int main(){
	int w,h;
	int v[4][2]={0,1,0,-1,1,0,-1,0};
	//cout<<v[0][0]<<" "<<v[0][1]<<endl;
	//return 0;
	//cin>>w>>h;
	/*
	rep(i,4){
		cout<<v[i][0]<<" "<<v[i][1]<<endl;
	}
	*/
	while(cin>>w>>h,w){
		memset(a,0,sizeof(a));
		memset(vis,0,sizeof(vis));
		rep(j,w-1){
			int t;
			cin>>t;
			//cout<<t<<" ";
			//cout<<t<<endl;
			if(t==1){
				a[0][j][0][j+1]=a[0][j+1][0][j]=1;
				//cout<<0<<" "<<j<<"  "<<0<<" "<<j+1<<endl;
			}
		}
		//cout<<endl;
		rep(i,h-1){
			rep(j,w){
				int t;
				cin>>t;
				//cout<<t<<" ";
				if(t==1){
					a[i][j][i+1][j]=a[i+1][j][i][j]=1;
					//cout<<i<<" "<<j<<"  "<<i+1<<" "<<j<<endl;
				}
			}
			//cout<<endl;
			rep(j,w-1){
				int t;
				cin>>t;
				//cout<<t<<" ";
				
				if(t==1){
					a[i+1][j][i+1][j+1]=a[i+1][j+1][i+1][j]=1;
					//cout<<i+1<<" "<<j<<"  "<<i+1<<" "<<j+1<<endl;
				}
			}
			//cout<<endl;
		}
		//cout<<endl;
		int t;
		//cout<<a[0][0][0][1]<<endl;
		//cout<<"a"<<a[0][0][0][1]<<" "<<a[0][0][1][0]<<" "<<a[0][1][0][0]<<" "<<a[1][0][0][0]<<endl;
		pii pos=mkp(0,0);
		queue<pii> q;
		q.push(pos);
		int ans=0;
		bool flag=false;
		vis[0][0]=1;
		while(!q.empty()){
			int qs=q.size();
			
			//cout<<pos.F<<" "<<pos.S<<endl;
			
			
			ans++;
			rep(k,qs){
				pii tq = q.front();
				q.pop();
				if(tq.F==h-1&&tq.S==w-1){
					cout<<ans<<endl;
					flag=true;
					break;
				}	
				rep(i,4){
					pii tpos=tq;
					tpos.F+=v[i][0];
					tpos.S+=v[i][1];
					//cout<<"tpos "<<tpos.F<<" "<<tpos.S<<endl;
					if(tpos.F>=0&&tpos.S>=0&&tpos.F<=h-1&&tpos.S<=w-1&&a[tq.F][tq.S][tpos.F][tpos.S]==0&&vis[tpos.F][tpos.S]==0){
						vis[tpos.F][tpos.S]=1;
						//cout<<"tpos "<<tpos.F<<" "<<tpos.S<<endl;
						q.push(tpos);
					}
				}
			}
		}
		if(!flag) cout<<0<<endl;
		
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    const int dirx[4] = {  1,  0, -1,  0 };
    const int diry[4] = {  0,  1,  0, -1 };

    while(true){
        int w, h;
        cin >> w >> h;
        if ( w == 0  &&  h == 0 )
            break;

        vector<string> wall(2*h-1); //このような場合はそのまま文字列として保存
        cin.ignore();
        for ( int i=0; i<2*h-1; i++ )
            getline( cin, wall[i] );

        vector<vector<int> > dist( h, vector<int>( w, 0 ) );
        dist[0][0] = 1;

        for ( int d=1; d<w*h; d++ ){
          for ( int y=0; y<h; y++ ){
            for ( int x=0; x<w; x++ ){
              if ( dist[y][x] == d ){
                for ( int r=0; r<4; r++ ){
                  int tx = x + dirx[r];
                  int ty = y + diry[r];

                  if ( 0 <= tx  &&  tx < w  &&
                       0 <= ty  &&  ty < h  &&
                      wall[ty+y][tx+x] == '0'  &&
                      dist[ty][tx] == 0 ){
                    dist[ty][tx] = d + 1;
                  }
                }
              }
            }
          }
        }

        cout << dist[h-1][w-1] << endl;
    }
}