#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <stack>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <bitset>

/*----------------------------------------------------------------*/

using ld = long double;
using vld = std::vector<ld>;
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using str = std::string;
using vstr = std::vector<str>;
using vb = std::vector<bool>;
using vvb = std::vector<vb>;

/*----------------------------------------------------------------*/

template<typename T> bool chmin(T a, T &b) { return a < b ? b = a, true : false; }
template<typename T> bool chmax(T a, T &b) { return a > b ? b = a, true : false; }
template<typename T> T sqr(T a) { return a * a; }

/*----------------------------------------------------------------*/

constexpr ld PI = 3.141592653589793L;
constexpr ld EPS = std::numeric_limits<ld>::epsilon();

/*----------------------------------------------------------------*/

ll gcd(ll n, ll m) { return m != 0 ? gcd(m, n % m) : n; }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }
bool is_prime(ll n) {
    if (n == 1) return false;
    for (ll i = 2; sqr(i) <= n; i++)
        if (n % i == 0) return false;
    return true;
}
ll popcnt(ll n) {
    n = ((n & 0xaaaaaaaaaaaaaaaa) >> 1) + (n & 0x5555555555555555);
    n = ((n & 0xcccccccccccccccc) >> 2) + (n & 0x3333333333333333);
    n = ((n & 0xf0f0f0f0f0f0f0f0) >> 4) + (n & 0x0f0f0f0f0f0f0f0f);
    n = ((n & 0xff00ff00ff00ff00) >> 8) + (n & 0x00ff00ff00ff00ff);
    n = ((n & 0xffff0000ffff0000) >> 16) + (n & 0x0000ffff0000ffff);
    n = ((n & 0xffffffff00000000) >> 32) + (n & 0x00000000ffffffff);
    return n;
}

/*----------------------------------------------------------------*/

const vll dy = { -1, 0, 1, 0 }, dx = { 0, 1, 0, -1 };
void solve();

/*----------------------------------------------------------------*/

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::cout << std::setprecision(15);

    solve();
}

void solve() {
    while (true) {
        ll w, h; std::cin >> w >> h;
        if (w == 0 && h == 0) return;
        vvll maze(2 * h - 1, vll(2 * w - 1, 0));
        for (ll i = 0; i < 2 * h - 1; i++)
            for (ll j = ~i & 1; j < 2 * w - 1; j += 2)
                std::cin >> maze[i][j];
        vvll dist(2 * h - 1, vll(2 * w - 1, 0));
        dist[0][0] = 1;
        std::deque<std::pair<ll, ll>> next;
        next.push_back({ 0, 0 });
        while (!next.empty()) {
            ll cy, cx;
            std::tie(cy, cx) = next.front();
            next.pop_front();
            for (ll i = 0; i < 4; i++) {
                ll ny = cy + 2 * dy[i], nx = cx + 2 * dx[i];
                if (ny < 0 || 2 * h - 2 < ny || nx < 0 || 2 * w - 2 < nx
                    || maze[cy + dy[i]][cx + dx[i]] == 1
                    || dist[ny][nx] != 0)
                    continue;
                dist[ny][nx] = dist[cy][cx] + 1;
                next.push_back({ ny, nx });
            }
        }
        std::cout << dist[2 * h - 2][2 * w - 2] << '\n';
    }
}



Measured memory_bytes: 3128