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;

//C++14 (GCC 5.4.1)

#define LLINF (1LL<<60)
typedef long long ll;
#define mod 1000000007

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define rrepd(i,a,b) for(int i=(a);i>=(b);i--)

int w,h,x;
/*←↓↑→ */
void moveto(int r,int x,int y,vector<vector<int>>&map,vector<vector<bool>>&kabe);

int main() {
    while(true){
        cin>> w>>h;
        if(w==0&&h==0)break;
        vector<vector<int>> map(w,vector<int>(h,INT_MAX));
        vector<vector<bool>> kabe(w,vector<bool>(2*h,true));
        rep(i,2*h-1){
            int cnt=(i%2) ? w : w-1;
            rep(k,cnt){
                cin>>x;
                if(x==0)kabe[k][i]=false;
            }
        }
        map[0][0]=1;
        moveto(1,0,0,map,kabe);
        moveto(3,0,0,map,kabe);
        cout<<((map[w-1][h-1]==INT_MAX) ? 0 : map[w-1][h-1])<<endl;
    }
    return (0);
}
int check[4][4]={{-1,0,-1,0},{0,1,0,1},{0,-1,0,-1},{1,0,0,0}};
void moveto(int r,int x,int y,vector<vector<int>> &map,vector<vector<bool>> &kabe){
    if(check[r][0]==-1&&x==0)return ;
    if(check[r][1]==-1&&y==0)return ;
    if(!kabe[x+check[r][2]][2*y+check[r][3]] && map[x][y]+1<map[x+check[r][0]][y+check[r][1]]){
        map[x+check[r][0]][y+check[r][1]]=map[x][y]+1;
        moveto(0,x+check[r][0],y+check[r][1],map,kabe);
        moveto(1,x+check[r][0],y+check[r][1],map,kabe);
        moveto(2,x+check[r][0],y+check[r][1],map,kabe);
        moveto(3,x+check[r][0],y+check[r][1],map,kabe);
    }



    return ;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int W, H;
int field[100][100];
bool visited[100][100];

void init() {
  fill(&field[0][0], &field[99][100], 1);
  fill(&visited[0][0], &visited[99][100], false);
}

typedef pair<int, int> P;

struct State {
  int cost, x, y;
  State(int cost, int x, int y) : cost(cost), x(x), y(y) {}
  void print() {
    printf("%d %d %d\n", cost, x - 2, y - 2);
  }
};

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

void solve() {
  queue<State> Q;
  Q.push(State(1, 1 + 2, 1 + 2));
  field[1 + 2][1 + 2] = true;
  while (!Q.empty()) {
    State cur = Q.front();
    Q.pop();
    //cur.print();

    int x = cur.x;
    int y = cur.y;
    
    if (cur.x == 2 * W + 1 && cur.y == 2 * H + 1) {
      printf("%d\n", cur.cost);
      return;
    }

    if (visited[cur.y][cur.x]) {
      continue;
    }
    visited[cur.y][cur.x] = true;
    
    for (int i = 0 ; i < 4; i++) {
      int nx = dx[i] + x;
      int ny = dy[i] + y;
      int wallx = wx[i] + x;
      int wally = wy[i] + y;
      if (field[wally][wallx] == 0 && !visited[ny][nx]) {
        Q.push(State(cur.cost + 1, nx, ny));
      }
    }
  }
  printf("%d\n", 0);
}

int main() {
  while (true) {
    scanf("%d %d ", &W, &H);
    init();
    if (W == 0 && H == 0) {
      return 0;
    }
    for (int i = 1; i <= 2 * H - 1; i++) {
      int t;
      if (i % 2 == 0) {
        t = W;
      } else {
        t = W - 1;
      }
      for (int j = 1; j <= t; j++) {
        int w;
        scanf("%d ", &w);
        if (i % 2 == 0) {
          field[i + 2][2 * j - 1 + 2] = w;
        } else {
          field[i + 2][2 * j + 2] = w;
        }
      }
    }
    solve();
  }
}