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 <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <map>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define FOR(i, b, e) for(int i = b; i < e; i++)
#define to_bit(i) static_cast< bitset<8> >(i)
#define INF (1<<28)
#define int(n) int n; cin >> n;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long>PLL;
typedef queue<int> QI;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int> > minpq;

struct edge{int to, cost;};
int gcd(int a, int b){if(a%b==0){return(b);}else{return(gcd(b,a%b));}};
int lcm(int m, int n){if((0 == m)||(0 == n)){return 0;} return ((m / gcd(m, n)) * n);};
unsigned long long comb(long n, long m){if(n < m) return 0;
    unsigned long long c = 1; m = (n - m < m ? n - m : m);
    for(long ns = n - m + 1, ms = 1; ms <= m; ns ++, ms ++){c *= ns; c /= ms;} return c;};
int quadratic(int a, int b, int c){return (int)(-b + sqrt(b*b - 4*a*c)) / 2*a;}

void cp(int from[], int to[], int l){REP(i, l) to[i] = from[i];};
void cp(string a1[], string a2[], int l){REP(i, l) a2[i] = a1[i];};
double sq(double d){return d*d;};
int sq(int i){return i*i;};
double sqdist(int x1, int y1, int x2, int y2){
    double dx = x1 - x2, dy = y1 - y2; return dx*dx + dy*dy;
};
bool inside(int y, int x, int h, int w){return 0 <= y && y <= (h-1) && 0 <= x && x <= (w-1);};


// 線分の交差判定
bool isCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
    // 並行な場合
    int m = (x2-x1)*(y4-y3)-(y2-y1)*(x4-x3);
    if(m == 0) return false;
    // 媒介変数の値が0より大きく1以下なら交差する、これは問題の状況によって変わるかも。
    double r =(double)((y4-y3)*(x3-x1)-(x4-x3)*(y3-y1))/m;
    double s =(double)((y2-y1)*(x3-x1)-(x2-x1)*(y3-y1))/m;
    return (0 < r && r <= 1 && 0 < s && s <= 1);
};

// 外積の計算 AB CD の内積を求める
int crossProdct(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy){
    int abx = bx - ax; int aby = by - ay;
    int cdx = dx - cx; int cdy = dy - cy;
    return abx * cdy - cdx * aby;
};
double crossProdct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy){
    double abx = bx - ax; double aby = by - ay;
    double cdx = dx - cx; double cdy = dy - cy;
    return abx * cdy - cdx * aby;
};
double innerProduct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy){
    double abx = bx - ax; double aby = by - ay;
    double cdx = dx - cx; double cdy = dy - cy;
    return abx * cdx + aby * cdy;
};

// 三角形の内部判定 ABCの中にPがあるか判定
bool inTriangle(int ax, int ay, int bx, int by, int cx, int cy, int px, int py){
    int c1 = crossProdct(ax, ay, bx, by, bx, by, px, py);
    int c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py);
    int c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py);
    return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0);
};
bool inTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py){
    double c1 = crossProdct(ax, ay, bx, by, bx, by, px, py);
    double c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py);
    double c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py);
    return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0);
};

// 三角形の外心
void circumcenter(double x1, double y1, double x2, double y2, double x3, double y3, double res[3]){
    double xp1 = (y1-y3)*(y1*y1-y2*y2+x1*x1-x2*x2) - (y1-y2)*(y1*y1-y3*y3+x1*x1-x3*x3);
    double xp2 = 2*(y1-y3)*(x1-x2)-2*(y1-y2)*(x1-x3);
    res[0] = xp1/xp2;
    double yp1 = (x1-x3)*(x1*x1-x2*x2+y1*y1-y2*y2) - (x1-x2)*(x1*x1-x3*x3+y1*y1-y3*y3);
    double yp2 = 2*(x1-x3)*(y1-y2)-2*(x1-x2)*(y1-y3);
    res[1] = yp1/yp2;
    double r = (x3-res[0])*(x3-res[0])+(y3-res[1])*(y3-res[1]);
    res[2] = sqrt(r);
}





/**
 * start
 * @author yoshikyoto
 */

int vy[4] = {-1, 1, 0, 0}, vx[4] = {0, 0, -1, 1};
int main(int argc, const char * argv[]){
    while (true) {
        int w, h;
        int maze[199][199], dist[199][199];
        cin >> w >> h;
        if(w + h == 0) break;
        int lw = 2*w-1, lh = 2*h-1;
        REP(i, lh) REP(j, lw){
            maze[i][j] = 0;
            if(i % 2 == 1 && j % 2 == 1) maze[i][j] = 1;
            dist[i][j] = -1;
        }
        
        REP(i, lh){
            if(i % 2){
                REP(j, w){
                    cin >> maze[i][2*j];
                }
            }else{
                REP(j, w-1){
                    cin >> maze[i][2*j+1];
                }
            }
        }
        
        /*
        REP(i, lh){
            REP(j, lw){
                cout << maze[i][j];
            }
            cout << endl;
        }*/
        
        queue<int> qy, qx, qcnt;
        dist[0][0] = 0;
        qy.push(0);
        qx.push(0);
        qcnt.push(0);
        while(!qy.empty()){
            int y = qy.front(); qy.pop();
            int x = qx.front(); qx.pop();
            int cnt = qcnt.front(); qcnt.pop();
            // cout << "bfs " << y << " " << x << " " << cnt << endl;
            
            REP(k, 4){
                int ny = y + vy[k];
                int nx = x + vx[k];
                if(inside(ny, nx, lh, lw) && !maze[ny][nx] && dist[ny][nx] == -1){
                    qy.push(ny);
                    qx.push(nx);
                    qcnt.push(cnt + 1);
                    dist[ny][nx] = cnt + 1;
                }
            }
        }
        
        /*
        cout << "dist" << endl;
        REP(i, lh){
            REP(j, lw){
                cout << dist[i][j];
            }
            cout << endl;
        }*/
        
        int ans = 0;
        if(dist[lh-1][lw-1] != -1) ans = dist[lh-1][lw-1] / 2 + 1;
        
        cout << ans << endl;
    }
}