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<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
typedef pair<int,int> P;
#define INF 1e9

int main(){
   ofstream outputfile("test.txt");
    int h,w;
	while(cin>>w>>h){
        if(w==0)break;
        //int k[2*h-1][w];
        int kx[h][w-1];
        int ky[h-1][w];
        int c=0,cc=0;
        memset(kx,0,sizeof(kx));
        memset(ky,0,sizeof(ky));
        for(int i=0;i<2*h-1;i++){
            if(i%2==0){
                for(int j=0;j<w-1;j++){
                    cin>>kx[c][j];
                }
                c++;
            }else{
                for(int j=0;j<w;j++){
                    cin>>ky[cc][j];
               }
                cc++;
            }
        }
        // for(int i=0;i<cc;i++){
        //     for(int j=0;j<w;j++)cout<<"ky:"<<ky[i][j]<<endl;
        // }
        int d[31][31];
        for(int i=0;i<31;i++)for(int j=0;j<31;j++)d[i][j]=INF;
        int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
        int sx=0,sy=0,gx=w-1,gy=h-1;
       // int m[h][w];memset(m,0,sizeof(0));
        queue<P> que;
        que.push(P(sy,sx));
        d[sy][sx]=0;
        while(que.size()){
            P p = que.front(); que.pop();
            if(p.first==gy&&p.second==gx)break;
            for(int i=0;i<4;i++){
                int ny = p.first + dy[i], nx = p.second + dx[i];
                //if(nx<0||ny<0)continue;
              //  cout<<"nx:"<<nx<<" ny:"<<ny<<" kx:"<<k[(ny+1)*2][nx-1]<<" ky:"<<k[(ny+1)*2-1][nx]<<endl;
                //if(k[(ny+1)*2][nx-1]==1)nx-=dx[i];
                //if(k[(ny+1)*2-1][nx]==1)ny-=dy[i];
                int tmp=0;
                if(dx[i]==-1||dy[i]==-1){
                    if(dx[i])tmp = 0;
                    else tmp = 0;
                }else{
                    if(dx[i])tmp = dx[i];
                    else tmp = dy[i];
                }
                if(dx[i]!=0&&kx[ny][nx-tmp]==1){
                     //cout<<"dn:"<<nx<<" "<<ny<<endl;
                    continue;
                }
                if(dy[i]!=0&&ky[ny-tmp][nx]==1){
                    // cout<<"nn:"<<nx<<" "<<ny-dy[i]<<endl;
                    // cout<<"kn:"<<ky[nx][ny-dy[i]]<<endl;
                     //cout<<"dny:"<<nx<<" "<<ny<<endl;
                    continue;
                }
                if(nx>=0&&nx<w&&0<=ny&&ny<h&&d[ny][nx]==INF){
                   //壁の判定
                  // cout<<"nx:"<<nx<<" ny:"<<ny<<" kx:"<<k[ny*2][nx-1]<<" ky:"<<k[(ny+1)*2-1][nx]<<endl;
                 // cout<<"nx:"<<nx<<" ny:"<<ny<<endl;
                   // if(k[ny*2][nx-1]!=1||k[ny*2-1][nx]!=1){
                     //   cout<<"n:"<<nx<<" "<<ny<<endl;
                     //cout<<"n:"<<nx<<" "<<ny<<" dx,dy:"<<dx[i]<<" "<<dy[i]<<" kx,ky:"<<kx[ny][nx-tmp]<<" ky:"<<ky[ny-tmp][nx]<<" d:"<<d[p.first][p.second] + 1<<endl;
                        que.push(P(ny,nx));
                        d[ny][nx] = d[p.first][p.second] + 1;
                   // }
                }
            }
        }

        // for(int i=0;i<h;i++){
        //     for(int j=0;j<w;j++){
        //         cout<<d[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }

        if(d[gy][gx]==INF)d[gy][gx]=0;
        else d[gy][gx]++;
        cout<<d[gy][gx]<<endl;
        outputfile<<d[gy][gx]<<endl;
    }
     outputfile.close();
}