// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  Point operator + (Point v) { return Point(x + v.x, y + v.y); }
  Point operator - (Point v) { return Point(x - v.x, y - v.y); }
  bool operator == (const Point &v) const {
    return x == v.x && y == v.y;
  }
  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

// 壁も１つのマス目として捉え直す.
// FREE: 壁がない壁. コストフリーで通れるマス. FREEからはBLANKにのみ遷移.
// BLANK: コスト1で通れるマス. BLANKからはFREEまたはBLANKへ遷移.
// WALL: 通れないマス.
enum Square {
  FREE = 0,
  BLANK = 1,
  WALL = 2
};
ostream & operator << (ostream & out, Square const & v) {
  if (v == 0) out << "0";
  else if (v ==  1) out << "o";
  else out << "+";
  return out;
}

#define MAX_W 62
#define INFTY (70*70)
Int W, H;
Point start = { 0, 0 }, goal;
vector<Point> DIRS = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
vector<vector<Int> > D(61, vector<Int>(61, INFTY));
vector<vector<Square> > G(MAX_W, vector<Square>(MAX_W, BLANK));


bool input() {
  cin >> W >> H;
  if (W == 0 && H == 0) return false;

  W = 2*W-1;
  H = 2*H-1;
  loop(h,0,H) loop(w,0,W) {
    G.at(h).at(w) = BLANK;
    D.at(h).at(w) = INFTY;
  }

  bool vert, wall;
  loop(h,0,H) {
    vert = h % 2 == 0;
    loop(w,0,W) {
      if (vert) {
        if (w%2==0) continue;
      } else {
        if (w%2==1) continue;
      }
      cin >> wall;
      if (wall) G.at(h).at(w) = WALL;
      else G.at(h).at(w) = FREE;
    }
  }

  goal = { W-1, H-1 };
  return true;
}

bool outOfRange(Point &p) {
  if (p.y < 0 || H <= p.y) return true;
  return p.x < 0 || W <= p.x;
}

bool isFree(Point &p) {
  return G.at(p.y).at(p.x) == FREE;
}

bool isWall(Point &p) {
  return G.at(p.y).at(p.x) == WALL;
}

// 最短経路中のBLANKを数える.
Int bfs() {
  queue<Point> Q;
  Q.push(start);
  D.at(start.y).at(start.x) = 1;
  while (Q.size()) {
    Point u = Q.front(); Q.pop(); // これは必ずBLANK
    Int prev_d = D.at(u.y).at(u.x);
    for (auto dir: DIRS) {
      Point pos = u + dir;
      if (outOfRange(pos)) continue;
      if (isWall(pos)) continue;
      if (isFree(pos)) pos = pos + dir; // これはBLANK確定
      Int &d = D.at(pos.y).at(pos.x);
      if (d <= prev_d + 1) continue; // すでにより最短経路で通過済み
      d = prev_d + 1;
      if (pos == goal) return d; // 幅優先探索では最初に到達した時が最短
      Q.push(pos);
    }
  }

  return 0; // Not found
}

void solve() {
  cout << bfs() << endl;
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}



Measured memory_bytes: 3112