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++: /*
ツ姪?路ツづ個催?短ツ仰猟猟」ツづ債、ツ閉敖優ツ静ヲツ探ツ催オツづ?嘉ーツつュツつアツづ?つェツづ?つォツづ慊つキツ。

ツづ慊つクツ、ツつアツづ個姪「ツ妥ィツづ?づ債、ツ禿シツ療債仕ツ様ツつェツ氾アツ湘ュツづ可姪環倒ツづ按形ツづ?づ按づ?づ?つ「ツづ慊つキツ。
ツづ按づゥツづ猟つュツ甘按単ツづ按渉按猟敖づ?、ツづ按づゥツづ猟つュツづュツつゥツづィツづ「ツつキツつ「ツデツーツタツ構ツ堕「ツづ可づ按づゥツづヲツつ、ツづ可陛渉環キツつオツつスツつ「ツづ?思ツつ、ツづ?つオツづ・ツつ、ツ。
ツ催?づ?甘按単ツづ?、ツづュツつゥツづィツづ「ツつキツつ「ツデツーツタツ構ツ堕「ツづ可づ按づゥツづ個づ債、ツ篠淞づ個づヲツつ、ツづ按陛渉環キツ陛サツ法ツづ?つキツ。


ツ?δ甘按単ツづ按陛渉環キツ法ツ??
ツ姪?路ツづ個陛?づ?ツづつづ個マツスツ姪堋づ?考ツつヲツづゥツ。
ツ篠淞づ個づヲツつ、ツづ按禿シツ療債づーツ考ツつヲツづゥツ。

 1
0 1
 0
1 0
 1

ツつアツづェツづーツ、ツ陛?づ?マツスツ姪堋づ?考ツつヲツづゥツづ?篠淞づ個づヲツつ、ツづ可陛渉環キツづ?つォツづゥツ。

010
011
000
110
010

ツ陛?つッツづゥツマツスツ姪堋づ可づ債、0ツづーツ鳴?づ淞づゥツ。
ツ陛?つッツづ按つ「ツマツスツ姪?ツ陛?づ「ツ、ツ陛?づ個古ーツ債キツ点)ツづ可づ債、1ツづーツ鳴?づ淞づゥツ。
ツつ?づ?づ債、ツ債カツ湘」ツつゥツづァツ右ツ可コツづ慊づ?閉敖優ツ静ヲツ探ツ催オツづーツつオツづ?、ツつサツづ個個仰嘉環づ個陛?青板づー2ツづ?環?づ?づ?1ツつキツづェツづ篠つ「ツつ「ツ。

 */
#include <iostream>
#include <queue>
#include <cstring>
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;
int t[100][100];
bool closed[100][100];

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

void solve(){
  queue<State> open;
  memset(closed, 0, sizeof(closed));
  open.push(State(0, 0, 0)); //(0,0)ツつゥツづァツコツスツト0ツづ?スツタツーツト

  //ツ行ツつッツづゥツづ?つアツづォツつェツづ按つュツづ按づゥツづ慊づ?繰ツづィツ陛板つキ
  while(!open.empty()){
    State st = open.front(); open.pop();

    //ツつキツづ?づ可辿ツづ?つスツつアツづ?づ個つ?づゥツ催?標ツづ按づァツ鳴ウツ篠仰つキツづゥ
    if(closed[st.y][st.x]) continue;
    closed[st.y][st.x] = true;

    //ツゴツーツδ仰づ慊づ?行ツつォツ陳?つ「ツつスツづァツコツスツトツづー2ツづ?環?づ?づ?1ツつオツつスツづ?づ個つェツ督堋つヲ
    if(st.y == h - 1 && st.x == w - 1){
      cout << st.cost / 2 + 1 << endl;
      return;
    }

    //4ツ陛サツ古シツづ鳴閉ェツ甘イ
    for(int i = 0; i < 4; i++){
      int nx = st.x + dx[i];
      int ny = st.y + dy[i];

      if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] == 0){
        open.push(State(nx, ny, st.cost + 1));
      }
    }
  }

  cout << 0 << endl;
}

int main(){
  while(cin >> w >> h, w || h){
    w = w * 2 - 1;
    h = h * 2 - 1;

    //ツマツッツプツ青カツ青ャ
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        //ツ縦ツづ個陛?づーツ禿シツ療債つキツづゥツ行ツづ?つ?づゥツづ?つォ
        if(i % 2 == 0){
          //2ツづ?環?づィツ静伉づェツづゥツ湘ェツ渉環づ債、ツ陛?つッツづゥツマツスツ姪?
          if(j % 2 == 0){
            t[i][j] = 0;
          }
          //2ツづ?環?づィツ静伉づェツづ按つ「ツ湘ェツ渉環づ債、ツ縦ツ陛?つェツつ?づゥツつゥツづ?つ、ツつゥツ禿シツ療債つウツづェツづゥ
          else{
            int x;
            cin >> x;
            t[i][j] = x;
          }
        }

        //ツ可。ツづ個陛?づーツ禿シツ療債つキツづゥツ行ツづ?つ?づゥツづ?つォ
        else{
          //2ツづ?環?づィツ静伉づェツづゥツ湘ェツ渉環づ債、ツ可。ツ陛?つェツつ?づゥツつゥツづ?つ、ツつゥツ禿シツ療債つウツづェツづゥ
          if(j % 2 == 0){
            int x;
            cin >> x;
            t[i][j] = x;
          }
          //2ツづ?環?づィツ静伉づェツづ按つ「ツ湘ェツ渉環づ債、ツ陛?づ個古ーツ債キツ点ツづ可づ按づゥツづ個づ?陛?つッツづ按つ「
          else{
            t[i][j] = 1;
          }
        }
      }
    }

    solve();
  }
}

C++: #include<iostream>
using namespace std;
//7:26

int main(){
	int w, h;
	while(1){
		int maizu[100][100] = {};
		cin>>w>>h;
		if(w == 0  && h == 0) break;
		for(int i = 0; i <= 2 * w; i++){
			maizu[0][i] = 1;
			maizu[h*2][i] = 1;
		}
		for(int i = 0; i <= 2 * h; i++){
			maizu[i][0] = 1;
			maizu[i][w*2] = 1;
		}
		
		for(int i = 1; i < 2 * h; i++){
			if(i%2 == 1){
				for(int j = 1; j < w; j++){
					cin>>maizu[i][j*2];
				}
			}else{
				for(int j = 1; j <= w; j++){
					cin>>maizu[i][j*2-1];
				}
			}
		}
		maizu[1][1] = 2; //maizu[2*h-1][2*w-1];
		
		
		while(1){
			int f = 0;
			for(int i = 1; i < 2 * h; i += 2){
				for(int j = 1; j < w * 2; j += 2){
					if(maizu[i][j] != 1 && maizu[i][j] != 0){
						if(maizu[i][j+1] == 0 && maizu[i][j+2] == 0){f++;
							maizu[i][j+2] = maizu[i][j]+1;
						}
						if(maizu[i][j-1] == 0 && maizu[i][j-2] == 0){f++;
							maizu[i][j-2] = maizu[i][j]+1;
						}
						if(maizu[i+1][j] == 0 && maizu[i+2][j] == 0){f++;
							maizu[i+2][j] = maizu[i][j]+1;
						}
						if(maizu[i-1][j] == 0 && maizu[i-2][j] == 0){f++;
							maizu[i-2][j] = maizu[i][j]+1;
						}
					}
				}
			}
			if(f == 0)break;
		}
		
		/*for(int i = 0; i <= 2 * h; i++){
			for(int j = 0; j <= 2 * w; j++){
				cout<<maizu[i][j];
			}cout<<endl;
		}*/
		
		if(maizu[2*h-1][2*w-1] != 0){
			cout<<maizu[2*h-1][2*w-1] - 1<<endl;	
		} else{
			cout<<0<<endl;
		}
		
		
		
	}
	return 0;
}