Problem:
Problem B:
Amazing Mazes
You are requested to solve maze problems.  Without passing through
these mazes, you might not be able to pass through the domestic 
contest!
A maze here is a rectangular area of a number of squares, lined up
both lengthwise and widthwise, The area is surrounded by walls except
for its entry and exit.  The entry to the maze is at the leftmost part
of the upper side of the rectangular area, that is, the upper side of
the uppermost leftmost square of the maze is open.  The exit is
located at the rightmost part of the lower side, likewise.
In the maze, you can move from a square to one of the squares
adjoining either horizontally or vertically.  Adjoining squares,
however, may be separated by a wall, and when they are, you cannot go
through the wall.
Your task is to find the length of the shortest path from the entry to
the exit.  Note that there may be more than one shortest paths, or
there may be none.
Input
The input consists of one or more datasets, each of which represents a
maze.
The first line of a dataset contains two integer numbers, the
width
w
and the height
h
of the rectangular area, in
this order.
The following 2 ×
h
− 1 lines of a dataset describe
whether there are walls between squares or not.  The first line starts
with a space and the rest of the line contains
w
− 1
integers, 1 or 0, separated by a space.  These indicate whether walls
separate horizontally adjoining squares in the first row.  An integer
1 indicates a wall is placed, and 0 indicates no wall is there.  The
second line starts without a space and contains
w
integers, 1 or 0,
separated by a space.  These indicate whether walls separate
vertically adjoining squares in the first and the second rows.  An
integer 1/0 indicates a wall is placed or not.  The following lines
indicate placing of walls between horizontally and vertically
adjoining squares, alternately, in the same manner.
The end of the input is indicated by a line containing two zeros.
The number of datasets is no more than 100.  Both the widths and the
heights of rectangular areas are no less than 2 and no more than 30.
Output
For each dataset, output a line having an integer indicating the
length of the shortest path from the entry to the exit.  The length of
a path is given by the number of visited squares.  If there exists
no path to go through the maze, output a line containing a single
zero.  The line should not contain any character other than this
number.
Sample Input
2 3
 1
0 1
 0
1 0
 1
9 4
 1 0 1 0 0 0 0 0
0 1 1 0 1 1 0 0 0
 1 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
 0 0 0 1 0 0 1 1
0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 1 0
12 5
 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0
 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0
 0 0 1 0 0 1 0 1 0 0 0
0 0 0 1 1 0 1 1 0 1 1 0
 0 0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 1 0 0
0 0
Output for the Sample Input
4
0
20

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define oke cout << "Yes" << '\n';
#define dame cout << "No" << '\n';
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using Hai2 = vector<vector<ll>>;
using HaiB = vector<vector<bool>>;
using Hai3 = vector<vector<vector<ll>>>;

int H,W;
queue<pair<int,int>>Q;
Hai2 dist(1100,vector<ll>(1100,1e9));
vector<int>gox = {1,0,-1,0};
vector<int>goy = {0,1,0,-1};
Hai2 tate(100,vector<ll>(100));
Hai2 yoko(100,vector<ll>(100));

void bfs(){
    while(!Q.empty()){
        int x,y;
        tie(y,x) = Q.front();
        Q.pop();
        rep(i,4){
            int xx=x+gox[i],yy=y+goy[i];
            if(xx < 0 || W <= xx || yy < 0 || H <= yy){
                continue;
            }
            if(i==0){
                if(tate[y][x]){
                    continue;
                }
            }
            if(i==1){
                if(yoko[y][x]){
                    continue;
                }
            }
            if(i==2){
                if(tate[y][x-1]){
                    continue;
                }
            }
            if(i==3){
                if(yoko[y-1][x]){
                    continue;
                }
            }
            if(dist[yy][xx] > dist[y][x]+1){
                dist[yy][xx] = dist[y][x]+1;
                Q.push({yy,xx});
            }
        }
    }
}

int main() {
	cout << fixed << setprecision(15);
    while(true){
        cin>>W>>H;
        if(W==0 && H==0){
            break;
        }
        rep(i,H*2-1){
            if(i%2==0){
                rep(j,W-1){
                    cin>>tate[i/2][j];
                }
            }else{
                rep(j,W){
                    cin>>yoko[i/2][j];
                }
            }
        }
        int a=1;
        dist[0][0]=1;
        Q.push({0,0});
        bfs();
        if(dist[H-1][W-1]==1e9){
            dist[H-1][W-1]=0;
        }
        cout<<dist[H-1][W-1]<<"\n";
        rep(i,H){
            rep(j,W){
                dist[i][j]=1e9;
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 12800
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using Board = vector<string>;
// using mint = modint998244353;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vp = vector<P>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vs = vector<string>;
using sl = set<ll>;
using Graph = vector<vector<ll>>;
using pq = priority_queue<ll>;
using pqg = priority_queue<ll, vector<ll>, greater<ll>>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define nall(a) a.begin(), a.end()
const ll INF = 1LL << 60;

int main(){
    while (true) {
        ll w, h; cin >> w >> h;
        if (w==0 && h==0) break;
        vvl ver(h, vl(w-1)), hor(h-1, vl(w));
        rep(i,h*2-1) {
            if (i%2==0) {
                rep(j,w-1) cin >> ver[i/2][j];
            } else {
                rep(j,w) cin >> hor[i/2][j];
            }
        }
        map<P,ll> dist;
        queue<P> que;
        dist[P(0,0)] = 0;
        que.push(P(0,0));
        while (!que.empty()) {
            ll ch = que.front().first, cw = que.front().second;
            que.pop();
            // up
            if (ch > 0 && !hor[ch-1][cw] && !dist.count(P(ch-1,cw))) {
                dist[P(ch-1,cw)] = dist[P(ch,cw)] + 1;
                que.push(P(ch-1,cw));
            }
            // down
            if (ch < h-1 && !hor[ch][cw] && !dist.count(P(ch+1,cw))) {
                dist[P(ch+1,cw)] = dist[P(ch,cw)] + 1;
                que.push(P(ch+1,cw));
            }
            // left
            if (cw > 0 && !ver[ch][cw-1] && !dist.count(P(ch,cw-1))) {
                dist[P(ch,cw-1)] = dist[P(ch,cw)] + 1;
                que.push(P(ch,cw-1));
            }
            // right
            if (cw < w-1 && !ver[ch][cw] && !dist.count(P(ch,cw+1))) {
                dist[P(ch,cw+1)] = dist[P(ch,cw)] + 1;
                que.push(P(ch,cw+1));
            }
        }
        if (dist.count(P(h-1,w-1))) cout << dist[P(h-1,w-1)]+1 << endl;
        else cout << 0 << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3440
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;


#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); ++i)
#define rrep(i,n) for(ll i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(a) a.rbegin(),a.rend()

inline void Yes() { std::cout << "Yes" << std::endl; }
inline void No() { std::cout << "No" << std::endl; }
inline void YN(bool flag) {flag ? Yes() : No();}

template<class T> bool chmin(T& a,T b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

template<class T> bool chmax(T& a,T b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T> using vc = std::vector<T>;
template<class T> using P = std::pair<T, T>;
template<typename T> using vv = vc<vc<T>>;
template<class T> using PQ = std::priority_queue<T, vc<T>>;
template<class T> using PQg = std::priority_queue<T, vc<T>, std::greater<T>>;
inline const std::vector<ll> dx = {1, 0, -1, 0,-1,-1,1,1};
inline const std::vector<ll> dy = {0, 1, 0, -1,-1,1,-1,1};
inline constexpr ll INF = 2e18;
using pll = std::pair<ll, ll>;

//座標がグリッド上にあるか
template<typename T>
bool Ongrid(T x,T y,T h,T w) {
    if(x >= 0 && x < h && y >= 0 && y < w) return true;
    else return false;
}

//グリッドを90度回転
template <typename T>
vv<T> rotate(const vv<T>& v) {
    if(v.empty() || v[0].empty()) return {};

    size_t n = v.size();
    size_t m = v[0].size();

    vv<T> nx(m,vc<T>(n));

    for (size_t i = 0; i < n; ++i)for (size_t j = 0; j < m; ++j) {
        nx[j][n-1-i] = v[i][j];
    }
    return nx;
}

template <typename T>
vv<T> counter_rotate(const vv<T>& v) {
    if(v.empty() || v[0].empty()) return {};

    size_t n = v.size();
    size_t m = v[0].size();

    vv<T> nx(m,vc<T>(n));

    for (size_t i = 0; i < n; ++i)for (size_t j = 0; j < m; ++j) {
        nx[m-1-j][i] = v[i][j];
    }
    return nx;
}

/*考察　幅優先探索で解く
*/
int main() {
  while(true) {
    ll W,H;cin>>W>>H;
    if(W==0&&H==0) break;
    vv<ll> seen(H,vc<ll>(W,-1));
    queue<pair<pll,ll>> que;
    vv<ll> wall(2*H,vc<ll>(W+1,1));

    rep(i,(H*2-1)) {
        if(i%2==0) {
            rep(j,W-1) {
                cin >> wall[i][j];
            }
         } else {
                rep(j,W) {
                    cin >> wall[i][j];
                }
            }
        }

    que.push({{0,0},0});seen[0][0]=0;
    while(!que.empty()) {
        ll x = que.front().first.first,y = que.front().first.second;
        ll d = que.front().second; que.pop();
        rep(i,4) {
            ll nx = x + dx[i],ny = y + dy[i];
            if(Ongrid(nx,ny,H,W) && seen[nx][ny] == -1) {
                if(wall[nx+x][min(ny,y)]) continue;
                que.push({{nx,ny},d+1});
                seen[nx][ny] = d+1;
                }
            }
        }
        cout << seen[H-1][W-1]+1 << endl;
    }
  }
accuracy: 1
time_ms: 10
memory_kb: 3196
final_rank: 6

code_snippet: // メモ
// C++コンテスト用マクロ
#include <bits/stdc++.h>
using namespace std;
using ll = long long; // 整数(long long型)
#define rep(i, n) for (ll i = 0; i < (n); i++) // 0~n-1まで
#define nall(a) a.begin(), a.end() // ノーマルオール
#define pb push_back // push_back
#define pob pop_back // pop_back
#define Yes cout << "Yes" << endl // Yes
#define No cout << "No" << endl // No
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG // 配列外参照時のエラー検出
#endif

int main(){
    vector<ll> ans;
    while(1){
        ll w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        map<pair<pair<ll, ll>, pair<ll, ll>>, ll> m;
        rep(i, 2 * h - 1){
            if(i % 2 == 0){
                rep(j, w - 1){
                    ll n;
                    cin >> n;
                    if(n == 0){
                        m[{{i / 2, j}, {i / 2, j + 1}}] = 1;
                        m[{{i / 2, j + 1}, {i / 2, j}}] = 1;
                    }
                }
            }
            else if(i % 2 == 1){
                rep(j, w){
                    ll n;
                    cin >> n;
                    if(n == 0){
                        m[{{i / 2, j}, {i / 2 + 1, j}}] = 1;
                        m[{{i / 2 + 1, j}, {i / 2, j}}] = 1;
                    }
                }
            }
        }
        vector<vector<ll>> used(h, vector<ll>(w, -1));
        queue<pair<pair<ll, ll>, ll>> q;
        q.push({{0, 0}, 1});
        used[0][0] = 1;
        while(!q.empty()){
            pair<ll, ll> current = q.front().first;
            ll d = q.front().second;
            q.pop();
            if(current.first == h - 1 && current.second == w - 1) break;
            vector<ll> di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};
            rep(i, 4){
                ll x = current.first + di[i], y = current.second + dj[i];
                if(x < 0 || x >= h || y < 0 || y >= w) continue;
                if(m.count({current, {x, y}}) != 0 && used[x][y] == -1){
                    used[x][y] = d + 1;
                    q.push({{x, y}, d + 1});
                }
            }
        }
        if(used[h - 1][w - 1] == -1){
            ans.push_back(0);
        }
        else{
            ans.push_back(used[h - 1][w - 1]);
        }
    }
    rep(i, (int)ans.size()){
        cout << ans[i] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3588
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
using ll = long long;
using ld = long double;
using pl = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
const int mod = 998244353;
const ll hashh = 2147483647;

int main()
{
    vector<pl> next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    while (1)
    {
        ll h, w;
        bool b = false;
        cin >> w >> h;
        if (h == 0 && w == 0)
            return 0;
        vvl a(h * 2 - 1, vl(w,1));
        vector<vector<bool>> flag(h, vector<bool>(w, false));
        vvl count(h, vl(w, 0));
        queue<pl> q;
        q.push({0, 0});
        flag[0][0] = true;
        count[0][0] = 1;
        rep(i, 0, h * 2 - 1)
        {
            rep(j, 0, w)
            {
                if (i % 2 == 0 && j == 0)
                {
                    continue;
                }
                cin >> a[i][j];
            }
        }
        while (!q.empty())
        {
            pl p = q.front();
            q.pop();
            if (p.first == h - 1 && p.second == w - 1)
            {
                b = true;
                cout << count[p.first][p.second] << endl;
                break;
            }
            rep(i, 0, 4)
            {
                ll nh = p.first + next[i].first;
                ll nw = p.second + next[i].second;
                if (nh < 0 || nh >= h || nw < 0 || nw >= w)
                    continue;
                if(next[i].second == -1){
                    if(a[p.first * 2 + next[i].first][nw+1] == 1) continue;
                }
                else{
                    if (a[p.first * 2 + next[i].first][nw] == 1) continue;
                }
                if (!flag[nh][nw])
                {
                    flag[nh][nw] = true;
                    count[nh][nw] = count[p.first][p.second] + 1;
                    q.push({nh, nw});
                }
            }
        }
        if (!b)
            cout << 0 << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3104
final_rank: 3

code_snippet: #include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
#define EPS (long double) 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;

int main() {
	while(1) {
		int w, h;
		cin>>w>>h;
		if(w == 0 && h== 0) break;
		int yoko[h][w - 1];
		int tate[h - 1][w];
		REP(i, 2 * h - 1) {
			if(i % 2 == 0) {
				REP(j, w - 1) cin>>yoko[i / 2][j];
			} else {
				REP(j, w) cin>>tate[i / 2][j];
			}
		}
		int d[h][w];
		REP(i, h) {
			REP(j, w) d[i][j] = INF;
		}
		d[0][0] = 0;
		queue<P> q;
		q.push(P(0, 0));
		while(!q.empty()) {
			P p = q.front();
			q.pop();
			if(p.first - 1 >= 0 && tate[p.first - 1][p.second] == 0 && d[p.first - 1][p.second] == INF) {
				d[p.first - 1][p.second] = d[p.first][p.second] + 1;
				q.push(P(p.first - 1, p.second));
			}
			if(p.first + 1 < h && tate[p.first][p.second] == 0 && d[p.first + 1][p.second] == INF) {
				d[p.first + 1][p.second] = d[p.first][p.second] + 1;
				q.push(P(p.first + 1, p.second));
			}
			if(p.second - 1 >= 0 && yoko[p.first][p.second - 1] == 0 && d[p.first][p.second - 1] == INF) {
				d[p.first][p.second - 1] = d[p.first][p.second] + 1;
				q.push(P(p.first, p.second - 1));
			}
			if(p.second + 1 < w && yoko[p.first][p.second] == 0 && d[p.first][p.second + 1] == INF) {
				d[p.first][p.second + 1] = d[p.first][p.second] + 1;
				q.push(P(p.first, p.second + 1));
			}
		}
		if(d[h - 1][w - 1] == INF) cout<<0<<endl;
		else cout<<d[h - 1][w - 1] + 1<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3068
final_rank: 1

code_snippet: #include <vector>
#include <iostream>
#include <queue>

using namespace std;

vector<int> dh = {1,-1,0,0};
vector<int> dw = {0,0,1,-1};

int main() {
  vector<int> res_list;
  while (true) {
    int W, H; cin >> W >> H;
    if (W == 0 && H == 0) break;
    vector<vector<vector<int>>> g(H, vector<vector<int>>(W, vector<int>(2))); //right, down
    for (int h = 0; h < 2 * H - 1; ++h) {
      if (h % 2 == 0) {
        //right
        for (int w = 0; w < W - 1; ++w) {
          cin >> g[h / 2][w][0];
        }
      }
      else {
        for (int w = 0; w < W; ++w) {
          cin >> g[h / 2][w][1];
        }
      }
    }

    queue<pair<int, int>> q;
    vector<vector<int>> dist(H, vector<int>(W, -1));
    q.push({0,0});
    dist[0][0] = 1;
    while(!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int h, w; h = p.first; w = p.second;
      for (int i = 0; i < 4; ++i) {
        int nh, nw; nh = h + dh[i]; nw = w + dw[i];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (dist[nh][nw] != -1) continue;
        if (i == 0 && g[h][w][1] == 1) continue; //down
        if (i == 1 && g[nh][nw][1] == 1) continue; //up
        if (i == 2 && g[h][w][0] == 1) continue; //right
        if (i == 3 && g[nh][nw][0] == 1) continue; //left
        q.push({nh,nw});
        dist[nh][nw] = dist[h][w] + 1;
      }
    }
    if (dist[H-1][W-1] == -1) dist[H-1][W-1] = 0;
    res_list.push_back(dist[H-1][W-1]);
  }
  for (int res: res_list) cout << res << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(T) T.begin(), T.end()
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const ll inf = 1e18;

void solve(ll w, ll h) {
    set<vector<ll>> st;
    rep(i, 2 * h - 1) {
        if (i % 2) {
            // よこ
            rep(j, w) {
                int n;
                cin >> n;
                if (n == 1) {
                    int ni = (i + 1) / 2;
                    P p1 = {ni, j};
                    P p2 = {ni - 1, j};
                    if (p2 < p1) swap(p1, p2);
                    st.insert({p1.first, p1.second, p2.first, p2.second});
                }
            }
        } else {
            // たて
            rep(j, w - 1) {
                int n;
                cin >> n;
                if (n == 1) {
                    int ni = i / 2;
                    P p1 = {ni, j};
                    P p2 = {ni, j + 1};
                    if (p2 < p1) swap(p1, p2);
                    st.insert({p1.first, p1.second, p2.first, p2.second});
                }
            }
        }
    }
    vector dist(h, vector<ll>(w, -inf));
    queue<pair<int, int>> q;
    q.push({0, 0});
    dist[0][0] = 1;
    while (q.size()) {
        auto [i, j] = q.front();
        q.pop();
        rep(v, 4) {
            int ni = i + di[v], nj = j + dj[v];
            if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
            if (dist[ni][nj] != -inf) continue;
            P p1 = {i, j};
            P p2 = {ni, nj};
            if (p2 < p1) swap(p1, p2);
            vector<ll> vec = {p1.first, p1.second, p2.first, p2.second};
            if (st.count(vec)) continue;
            q.push({ni, nj});
            dist[ni][nj] = dist[i][j] + 1;
        }
    }
    ll ans = 0;
    if (dist[h - 1][w - 1] != -inf) ans = dist[h - 1][w - 1];
    cout << ans << '\n';
}

int main() {
    while (1) {
        ll w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        solve(w, h);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<int> d_tate = {1, -1, 0, 0};
vector<int> d_yoko = {0, 0, 1, -1};
//0は↑、1は↓、2は→、3は←をあらわす

int main() {
    int H, W;
    while(true){
        cin >> W >> H;
        if(H == 0 && W == 0){
            return 0;
        }
        vector<vector<int>> tatekabe(H, vector<int>(W-1));
        vector<vector<int>> yokokabe(H-1, vector<int>(W));
        for(int i = 0; i < H; i++){
            if(i == H-1){
                for(int j = 0; j < W-1; j++){
                    cin >> tatekabe[i][j];
                }
            }
            else{
                for(int j = 0; j < W-1; j++){
                    cin >> tatekabe[i][j];
                }
                for(int j = 0; j < W; j++){
                    cin >> yokokabe[i][j];
                }
            }
        }
        //縦壁横壁出力（合ってた）
        /*
        cout << "tatekabe=" << endl;
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W-1; j++){
                cout << tatekabe[i][j];
            }
            cout << endl;
        }
        cout << "yokokabe=" << endl;
        for(int i = 0; i < H-1; i++){
            for(int j = 0; j < W; j++){
                cout << yokokabe[i][j];
            }
            cout << endl;
        }
        */

        vector<vector<int>> dist(H, vector<int>(W, -1));
        queue<pair<int, int>> q;

        dist[0][0] = 1; //dist[i][j]は、iが縦行の場所で、jが横行の場所
        q.push(make_pair(0, 0));

        //ここまではOK
        while(!q.empty()){
            int tate = q.front().first, yoko = q.front().second;
            q.pop();

            for(int i = 0; i < 4; i++){
                int n_tate = tate + d_tate[i];
                int n_yoko = yoko + d_yoko[i];
                //はみ出してないか、探索済みでないか
                if(n_tate<0 || n_tate>=H || n_yoko<0 || n_yoko>=W){
                    continue;
                }
                if(dist[n_tate][n_yoko] != -1){
                    continue;
                }
                //0,1なら上下移動なのでyokokabeを見る
                if(i == 0 || i == 1){
                    if(yokokabe[min(tate, n_tate)][n_yoko] == 1){
                        continue;
                    }
                }
                else{
                    if(tatekabe[n_tate][min(yoko, n_yoko)] == 1){
                        continue;
                    }
                }

                dist[n_tate][n_yoko] = dist[tate][yoko] + 1;
                q.push(make_pair(n_tate, n_yoko));
            }
        }

        if(dist[H-1][W-1] == -1){
            cout << 0 << endl;
        }
        else{
            cout << dist[H-1][W-1] << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3092
final_rank: 2

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}// if(a==b)YN;
#define NO2 cout<<-1<<endl

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i=int(n)-1; i>=0; --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

int dh[] = {0,1,0,-1}, dw[] = {1,0,-1,0};
int INF = int(1e9);

void solve(int H, int W) {
    vector<vector<int>> dist(H, vector<int>(W, INF));
    dist[0][0] = 1;
    vector<vector<int>> wall_h(H-1, vector<int>(W)), wall_v(H, vector<int>(W-1));
    rep(i, 2*H-1) {
        if (i%2 == 0) {
            rep(j, W-1) {
                cin >> wall_v[i/2][j];
            }
        }
        else {
            rep(j, W) {
                cin >> wall_h[i/2][j];
            }
        }
    }
    queue<pair<int,int>> que;
    que.push({0,0});
    while(que.size()) {
        auto [h,w] = que.front(); que.pop();
        rep(k,4) {
            int nh=h+dh[k], nw=w+dw[k];
            if (nh<0||H<=nh||nw<0||W<=nw) continue;
            if (dist[nh][nw] != INF) continue;
            if (dh[k] == 1) {
                if (wall_h[h][w]) continue;
            }
            else if (dh[k] == -1) {
                if (wall_h[nh][w]) continue;
            }
            else if (dw[k] == 1) {
                if (wall_v[h][w]) continue;
            }
            else {
                if (wall_v[h][nw]) continue;
            }

            if (dist[h][w]+1 < dist[nh][nw]) {
                dist[nh][nw] = dist[h][w]+1;
                que.push({nh,nw});
            }
        }
    }
    if (dist[H-1][W-1] != INF) cout << dist[H-1][W-1] << endl;
    else cout << 0 << endl;

    /*
    cout << "--- MAZE ---\n";
    rep(i,H) {
        rep(j,W) {
            if (dist[i][j] == INF) cout << "* ";
            else cout << dist[i][j] << ' ';
        }
        cout << "\n";
    }
    */
}

int main() {
    while(true) {
        int H, W;
        cin >> W >> H;
        if (H==0 && W==0) break;
        solve(H,W);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3248
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <tuple>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()

ll llpow(ll a, ll t)
{
  ll res = 1;
  rep(i, t) res *= a;
  return res;
}

ll inf = std::numeric_limits<ll>::max();

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};
vector<ll> dgx = {1, -1, 0, 0};
vector<ll> dgy = {0, 0, 1, 0};

vector<ll> ans;

int main()
{
  while (true)
  {
    ll w, h;
    cin >> w >> h;
    if (!w && !h)
    {
      break;
    }
    vector<vector<ll>> g(2 * h - 1, vector<ll>(w, 0));
    rep(i, 2 * h - 1)
    {
      if (i % 2)
      {
        rep(j, w)
        {
          cin >> g[i][j];
        }
      }
      else
      {
        rep(j, w - 1)
        {
          cin >> g[i][j + 1];
        }
      }
    }
    vector<vector<ll>> dsts(h, vector<ll>(w, inf));
    dsts[0][0] = 1;
    queue<vector<ll>> q;
    q.push({0, 0});
    while (q.size())
    {
      ll ux = q.front()[0];
      ll uy = q.front()[1];
      q.pop();
      rep(i, 4)
      {
        ll vx = ux + dx[i];
        ll vy = uy + dy[i];
        if (vx < 0 || vx >= h || vy < 0 || vy >= w)
        {
          continue;
        }
        if (g[2 * ux + dgx[i]][uy + dgy[i]])
        {
          continue;
        }
        if (dsts[ux][uy] + 1 < dsts[vx][vy])
        {
          dsts[vx][vy] = dsts[ux][uy] + 1;
          q.push({vx, vy});
        }
      }
    }
    if (dsts[h - 1][w - 1] == inf)
    {
      ans.push_back(0);
    }
    else
    {
      ans.push_back(dsts[h - 1][w - 1]);
    }
  }
  rep(i, ans.size())
  {
    cout << ans[i] << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3488
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;


template<typename T> using vc = vector<T>;//prioriy_queueに必要なのでここにこれ書いてます
template<typename T> using vv = vc<vc<T>>;

//-------------1.型系---------------
using ll = long long;
using ull = unsigned long long;
ll INF = 3e18;
double pi = 3.14159265358979;
using ld = long double;
using bl = bool;
//using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;//使うときはコメントアウトを外す
//mint::set_mod(m);//使うときはコメントアウトを外す

template<class T> using pq = priority_queue<T, vc<T>>;//大きい順
template<class T> using pq_g = priority_queue<T, vc<T>, greater<T>>;//小さい順
//-----------------------------------



//-------------2.配列系--------------
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>; using vvvs = vv<vs>;
using vb = vc<bl>; using vvb = vv<bl>; using vvvb = vv<vb>;
using vld = vc<ld>; using vvld = vv<ld>; using vvvld = vv<vld>;
using pll = pair<ll, ll>;
//using vmint = vc<mint>; using vvmint = vv<mint>; using vvvmint = vv<vmint>;

#define rep(i,n) for(ll i = 0; i < (n); ++i)//↓でrepを使うので書いてます
template<class T>istream& operator>>(istream& i, vc<T>& v) { rep(j, size(v))i >> v[j]; return i; }

using ar2 = array<ll, 2>;

//----------------------------------



//--------3.コード短縮化とか---------
#define rep(i,n) for(ll i = 0; i < (n); ++i)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)

#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back


#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}// if(a==b)YN;
#define dame cout<<-1<<endl


#define vc_unique(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define vc_rotate(v) rotate(v.begin(),v.begin()+1,v.end());

#define pop_cnt(s) ll(popcount(uint64_t(s)))

#define next_p(v) next_permutation(v.begin(),v.end())

ll nc2(ll x) { return x * (x - 1) / 2; }
ll nc3(ll x) { return x * (x - 1) * (x - 2) / 6; }

#define yu_qurid(x,y) ((x)*(x)+(y)*(y))//ユークリッド距離 sqrtはしてないなので注意
#define mannhattan(x1,x2,y1,y2) (abs(x1-x2)+abs(y1-y2)) // マンハッタン距離 = |x1-x2|+|y1-y2|

//if (regex_match(s, regex("")))YN;//文字列sの判定を行う。コメントアウトを外して「""」の中に判定する内容を入れる

//-------------------------------




//---------4.グリッド系----------
vl dx = { 1,0,-1,0 };
//vl dx={1,1,0,-1,-1,-1,0,1};
vl dy = { 0,1,0,-1 };
//vl dy={0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w) {//trueならcontinue
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

#define vvl_kaiten(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[j][n-i-1]=v[i][j];swap(nx,v);}//時計回りに90°回転
//#define vvl_kaiten(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//反時計周りに90°回転

#define vs_kaiten(v) {ll n = size(v);vs nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[j][n-i-1]=v[i][j];swap(nx,v);}//文字列版 時計回りに90°回転
//#define vs_kaiten(v) {ll n = size(v);vs nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//文字列版　反時計周りに90°回転


#define vvl_tenti(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[j][i]=v[i][j];swap(nx,v);}
#define vs_tenti(v) {ll n = size(v); vs nx(n, string(n,'.')); rep(i, n)rep(j, n)nx[j][i] = v[i][j]; swap(nx, v);}

//回転、転置はn*nしか使えないことに注意

//--------------------------------

int main() {
    while(1){
        ll w, h;
        cin >> w >> h;
        if(w == 0 and h == 0) return 0;

        vvl x(h, vl(w-1));
        vvl y(h-1, vl(w));
        rep(i, 2*h-1){
            if(i%2 == 0) {
                rep(j, w-1) cin >> x[i/2][j];
            }
            else {
                rep(j, w) cin >> y[i/2][j];
            }
        }

        vvl g(h*w);
        rep(i, h)rep(j, w-1){
            if(x[i][j]) continue;
            g[w*i+j].pb(w*i+j+1);
            g[w*i+j+1].pb(w*i+j);
        }
        rep(i, h-1)rep(j, w){
            if(y[i][j]) continue;
            g[w*i+j].pb(w*(i+1)+j);
            g[w*(i+1)+j].pb(w*i+j);
        }

        vl dist(w*h);
        dist[0] = 1;
        queue<ll> q;
        q.push(0);
        while(q.size()){
            ll v = q.front(); q.pop();
            for(ll nex : g[v]){
                if(dist[nex] != 0) continue;
                dist[nex] = dist[v]+ 1;
                q.push(nex);
            }
        }

        cout << dist[h*w-1] << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3492
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main() {
    while(true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        int dh[4] = {0, -1, 0, 1};
        int dw[4] = {1, 0, -1, 0};
        bool dir[35][35][4];
        
        const int INF = 1e9;
        
        vector<vector<int>> d(h, vector<int>(w, INF));
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                for (int k = 0; k < 4; k++) dir[i][j][k] = false;
            }
        }
        
        for (int i = 0; i < 2 * h - 1; i++) {
            if (i % 2 == 0) {
                for (int j = 0; j < w - 1; j++) {
                    int t;
                    cin >> t;
                    if (t == 0) {
                        dir[i / 2][j][0] = true;
                        dir[i / 2][j + 1][2] = true;
                    }
                }
            }
            else {
                for (int j = 0; j < w; j++) {
                    int t;
                    cin >> t;
                    if (t == 0) {
                        dir[i / 2][j][3] = true;
                        dir[(i / 2) + 1][j][1] = true;
                    }
                }
            }
        }
        
        queue<pair<int, int>> Q;
        Q.push({0, 0});
        d[0][0] = 0;
        
        while (!Q.empty()) {
            auto p = Q.front();
            Q.pop();
            int h = p.first;
            int w = p.second;
            for (int i = 0; i < 4; i++) {
                if (!dir[h][w][i]) continue;
                int nh = h + dh[i];
                int nw = w + dw[i];
                if (d[nh][nw] > d[h][w] + 1) {
                    d[nh][nw] = d[h][w] + 1;
                    Q.push({nh, nw});
                }
            }
        }
        
        if (d[h - 1][w - 1] == INF) cout << 0 << endl;
        else cout << d[h - 1][w - 1] + 1 << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3392
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;
ll myceil(ll a, ll b) {return (a+b-1)/b;}

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

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T>>& vv){ for(const auto& v : vv){ view(v); } }


int w,h;

void solve() {
    vector<vector<int>> tate(h,vector<int> (w-1));
    vector<vector<int>> yoko(h-1,vector<int> (w));

    rep(i,2*h-1) {
        if (i%2) {
            rep(j,w) {
                cin >> yoko[i/2][j];
            }
        }
        else {
            rep(j,w-1) {
                cin >> tate[i/2][j];
            }
        }
    }

    vector<vector<int>> v(h*w);
    rep(i,h*w) {
        int x = i/w;
        int y = i%w;


        if (y != w-1) {
            if (tate[x][y] == 0) {
                v[i].push_back(i+1);
                v[i+1].push_back(i);
            }
        }
        
        if (x != h-1) {
            if (yoko[x][y] == 0) {
                v[i].push_back(i+w);
                v[i+w].push_back(i);
            }
        }
    }

    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
    vector<int> dist(h*w,10000000);
    dist[0] = 1;
    pq.push({1,0});

    while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();

        int ct = p.first;
        int f = p.second;

        for (auto x : v[f]) {
            if (dist[x] > ct+1) {
                dist[x] = ct+1;
                pq.push({ct+1,x});
            }
        }
    }


    if (dist[h*w-1] == 10000000) {
        cout << 0 << endl;
    }
    else {
        cout << dist[h*w-1] << endl;
    }
    return;
}


int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);


    while (cin >> w >> h && w != 0) {
        solve();
    }

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

vector<char> isprime;
void init() {
    isprime.assign(1000005, 1);
    isprime[0] = 0;
    isprime[1] = 0;
    for(int i = 2; i < 1000005; i++) {
        if(isprime[i]) {
            for(int j = i * 2; j < 1000005; j += i) {
                isprime[j] = 0;
            }
        }
    }
}

ll w, h;
void input() { cin >> w >> h; }
void solve() {
    vector<vector<ll>> g(h * w);
    for(int i = 0; i < 2 * h - 1; i++) {
        if(i % 2) {
            for(int j = 0; j < w; j++) {
                ll a;
                cin >> a;
                if(a == 0) {
                    ll p = (i / 2) * w + j, q = (i / 2 + 1) * w + j;
                    g[p].push_back(q);
                    g[q].push_back(p);
                }
            }
        } else {
            for(int j = 0; j < w - 1; j++) {
                ll a;
                cin >> a;
                if(a == 0) {
                    ll p = (i / 2) * w + j, q = (i / 2) * w + j + 1;
                    g[p].push_back(q);
                    g[q].push_back(p);
                }
            }
        }
    }

    vector<ll> dist(h * w, 1e18);
    dist[0] = 0;
    queue<ll> que;
    que.push(0);
    while(que.size()) {
        ll f = que.front();
        que.pop();
        for(auto &x : g[f]) {
            if(dist[x] > dist[f] + 1) {
                dist[x] = dist[f] + 1;
                que.push(x);
            }
        }
    }

    cout << (dist[h * w - 1] == 1e18 ? 0 : dist[h * w - 1]+1) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // init();
    while(1) {
        input();
        if(h == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool solve() {
    int H, W;
    cin >> W >> H;
    if (H == 0 && W == 0) return false;
    vector<vector<int>> R(H, vector<int>(W - 1));
    vector<vector<int>> D(H - 1, vector<int>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W - 1; j++) cin >> R[i][j];
        if (i < H - 1) {
            for (int j = 0; j < W; j++) cin >> D[i][j];
        }
    }
    vector<vector<int>> dist(H, vector<int>(W, 1 << 30));
    dist[0][0] = 1;
    queue<pair<int, int>> q;
    q.emplace(0, 0);
    while (q.size()) {
        auto [r, c] = q.front();
        q.pop();
        for (int d = 0; d < 4; d++) {
            int nr = r + vector<int>{0, 1, 0, -1, 0}[d];
            int nc = c + vector<int>{0, 1, 0, -1, 0}[d + 1];
            if (0 <= nr && nr < H && 0 <= nc && nc < W) {
                if (r == nr) {
                    if (R[r][min(c, nc)] == 0) {
                        if (dist[nr][nc] > dist[r][c] + 1) {
                            dist[nr][nc] = dist[r][c] + 1;
                            q.emplace(nr, nc);
                        }
                    }
                } else {
                    if (D[min(r, nr)][c] == 0) {
                        if (dist[nr][nc] > dist[r][c] + 1) {
                            dist[nr][nc] = dist[r][c] + 1;
                            q.emplace(nr, nc);
                        }
                    }
                }
            }
        }
    }
    int ans = dist.back().back();
    if (ans == 1 << 30) ans = 0;
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 16

code_snippet: #include<bits/stdc++.h>
//入力系
#define cinll(...) ll __VA_ARGS__; input(__VA_ARGS__);
#define cinint(...) int __VA_ARGS__; input(__VA_ARGS__);
#define cinstr(...) string __VA_ARGS__; input(__VA_ARGS__);
#define cinchar(...) char __VA_ARGS__; input(__VA_ARGS__);
#define cindouble(...) double __VA_ARGS__; input(__VA_ARGS__);
#define cinvll(a,n) vll a(n); rep(i,n) cin>>a[i];
#define cinvvll(a,n,m) vvll a(n,vll(m)); rep(i,n) rep(j,m) cin>>a[i][j];
#define cinvs(a,n) vs a(n); rep(i,n) cin>>a[i];
//繰り返し系
#define rep1(n) for(ll i=0;i<n;i++)
#define rep2(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,n) for(ll i=a;i<n;i++)
#define rep4(i,a,n,b) for(ll i=a;i<n;i+=b)
#define overload4(a,b,c,d,e,...) e
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define mrep1(n) for(ll i=n;i>=0;i--)
#define mrep2(i,n) for(ll i=n;i>=0;i--)
#define mrep3(i,n,a) for(ll i=n;i>=a;i--)
#define mrep4(i,n,a,b) for(ll i=n;i>=a;i-=b)
#define mrep(...) overload4(__VA_ARGS__,mrep4,mrep3,mrep2,mrep1)(__VA_ARGS__)
//iterator系
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
//書くのが長いやつ
#define fst first
#define snd second
using namespace std;
//型系
using ll = long long;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using pll = pair<long long,long long>;
using sll = set<long long>;
using vsll = vector<set<long long>>;
using vpll = vector<pair<long long,long long>>;
using vvpll  = vector<vector<pair<long long, long long>>>;
#define vmll vector<mll>
#define vvmll vector<vector<mll>>

//const ll mod = 998244353LL;
const ll mod = 1000000007LL;
const ll inf = 1301001001001001000LL;
const double PI=3.1415926535897932384626433832795028841971;

//表示
#define overload1(a,b,NAME,...) NAME
#define coutYesReturn() do {coutYes(); return 0; } while(0)
#define coutYesReturnIf(a) do { if(a){ coutYesReturn(); }} while(0)
#define coutNoReturn() do {coutNo(); return 0;} while(0)
#define coutNoReturnIf(a) do {if(a){ coutNoReturn(); }} while(0)
#define coutReturnIf(a,s) do{if(a){cout<<s<<endl; return 0;}}while(0)
template<typename... T>
void coutll(T... a){ ((cout << a <<" "),...) << endl; }
void coutvll(vll &a){ rep(i,a.size()) cout<<a[i]<<" "; cout<<endl; }
void coutvll(string name, vll &a){ cout<<name<<":"; coutvll(a); }
void coutvlln(vll &a){ rep(i,a.size()) cout<<a[i]<<endl; }
void coutYes(){ cout<<"Yes"<<endl; }
void coutNo(){ cout<<"No"<<endl; }
void coutYesNo(bool a){ cout<<(a?"Yes":"No")<<endl; }
void coutIf(bool a, string s, string t){ cout<<(a?s:t)<<endl; }
//入力
template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}
//複数ソート
template<class... T>
void sorts(vector<T>&... a){
    (sort(all(a)),...);
}
template<typename T>
T chmax(T &a, T b){ if(a < b) a = b; return a; }
template<typename T>
T chmin(T &a, T b){ if(a > b) a = b; return a; }

//縦0以上h未満、横0以上w未満の(i,j)の4近傍をとる
vpll kinbo4(ll h,ll w, ll i,ll j){
    vpll ret;
    if(i-1 >= 0) ret.push_back({i-1,j});
    if(j-1 >= 0) ret.push_back({i,j-1});
    if(i+1 < h) ret.push_back({i+1,j});
    if(j+1 < w) ret.push_back({i,j+1});
    return ret;
}

int main(){
    while(true){
    cinll(w,h);
    if(w+h == 0) break;
    vvll nexts(h*w);
    rep(i,h*2-1){
        if(i%2 == 0){
            rep(j,w-1){
                cinll(t);
                if(t == 0){
                    nexts[i/2*w + j].push_back(i/2*w + j+1);
                    nexts[i/2*w + j+1].push_back(i/2*w + j);
                }
            }
        }
        else{
            rep(j,w){
                cinll(t);
                if(t == 0){
                    nexts[i/2*w + j].push_back((i/2+1)*w + j);
                    nexts[(i/2+1)*w + j].push_back(i/2*w + j);
                }
            }
        }
    }
    //型を入力
    queue<ll> q;
    
    //ここでスタート地点を入れてく
    vll d(h*w ,-1);
    q.push(0);
    d[0] = 0;
    
    while(!q.empty()){
        ll now = q.front();
        q.pop();
    
        //ここで次の奴らを追加していく
        for(ll next: nexts[now]) if(d[next] == -1){
            d[next] = d[now]+1;
            q.push(next);
        }
    }

    cout<<d[h*w-1]+1<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using namespace atcoder;
// if using this, use "g++ *.cpp -I ~/atcoder/ac..." to complile

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (int i = 0; i < (n); i++)
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
const int IINF = 1e9;
const ll LINF = 1ll<<60; 

// main
int main()
{
	while (1) {
		int h, w;
		cin >> w >> h;
		
		if (w == 0 && h == 0) break;

		vector<vector<vector<pair<int, int>>>> G(h, vector<vector<pair<int, int>>>(w));
		rep(i, h) {
			rep(j, w-1) {
				int k1; cin >> k1;
				if (k1 == 0) {
					G[i][j].push_back({i, j+1});
					G[i][j+1].push_back({i, j});
				}
			}
			
			if (i == h-1) continue;

			rep(j, w) {
				int k; cin >> k;
				if (k == 0) {
					G[i][j].push_back({i+1, j});
					G[i+1][j].push_back({i, j});
				}
			}
		}

		vector<vector<int>> dist(h, vector<int>(w, IINF));
		dist[0][0] = 0;
		queue<pair<int, int>> que;
		que.push({0, 0});

		while (!que.empty()) {
			auto [x, y] = que.front();
			que.pop();

			for (auto [nx, ny]: G[x][y]) {
				if (dist[x][y]+1 < dist[nx][ny]) {
					dist[nx][ny] = dist[x][y]+1;
					que.push({nx, ny});
				}
			}
		}

		if (dist[h-1][w-1] == IINF) {
			cout << 0 << "\n";
			continue;
		}
		cout << dist[h-1][w-1]+1 << "\n";
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3548
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

void bfs(vector<vector<int>> &mass, queue<int> &Q){
  vector<vector<int>> dr;
  int W, H;
  W = mass.at(0).size();
  H = mass.size();
  dr = {{-1, 0},
        {0, 1},
        {1, 0},
        {0, -1}};
        
  for(int i = 0; i < 4; i++){
    int dx, dy;
    dx = Q.front() % 100 + dr.at(i).at(1);
    dy = Q.front() / 100 + dr.at(i).at(0);
    //cout << dx << " " << dy;
    if(dx >= 0 && dx < W && dy >= 0 && dy < H){
      //cout << "o";
      if(mass.at(dy).at(dx) == 0 && mass.at(dy + dr.at(i).at(0)).at(dx + dr.at(i).at(1)) > mass.at(Q.front() / 100).at(Q.front() % 100) + 1){
        dy += dr.at(i).at(0);
        dx += dr.at(i).at(1);
        mass.at(dy).at(dx) = mass.at(Q.front() / 100).at(Q.front() % 100) + 1;
        Q.push(dy * 100 + dx);
        //cout << endl;
      }
    }
    //cout << endl;
  }
}

int main(){
  int w, h, mgn = 1;
  while(mgn == 1){
  cin >> w >> h;
  if(w == 0 && h == 0){
    return 0;
  }
  vector<vector<int>> hw(h * 2 - 1, vector<int>(w * 2 - 1, 100000));
  for(int i = 0; i < w - 1; i++){
    cin >> hw.at(0).at(i * 2 + 1);
  }
  for(int i = 0; i < h - 1; i++){
    for(int j = 0; j < w ; j++){//横の壁奇数行目偶数列目
      cin >> hw.at(i * 2 + 1).at(j * 2);
    }
    for(int j = 0; j < w - 1; j++){
      cin >> hw.at(i * 2 + 2).at(j * 2 + 1);
    }
  }
    /*for(int i = 0; i < 2 * h - 1; i++){
    for(int j = 0; j < 2 * w - 1; j++){
      cout << hw.at(i).at(j) << " ";
    }
    cout << endl;
  }*/
  hw.at(0).at(0) = 1;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    bfs(hw, q);
    q.pop();
  }
  
  /*for(int i = 0; i < 2 * h - 1; i++){
    for(int j = 0; j < 2 * w - 1; j++){
      cout << hw.at(i).at(j) << " ";
    }
    cout << endl;
  }*/
  if(hw.at(h * 2 - 2).at(w * 2 - 2) == 100000){
    cout << 0 << endl;
  }
  else{
    cout << hw.at(h * 2 - 2).at(w * 2 - 2) << endl;
  }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int  i = 0; i < (n); ++i)
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
const long long INF = 1LL << 60;
using Graph = vector<vector<int>>;
int gcd(int a, int b){
    if(a%b == 0) return b;
    else return gcd(b, a%b);
}
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main(){
    while(1){
        int w,h; cin >> w >> h;
        if(w==0 && h==0) return 0;
        vector<vector<int>> f(2*h-1, vector<int>(2*w-1,1e9));
        for(int i=0; i<2*h-1;i++){
            for(int j=0;j<2*w-1;j++){
                if(i%2==0){
                    if(j%2==1) cin >> f[i][j];
                }else{
                    if(j%2==0) cin >> f[i][j];
                }
            }
        }
        queue<pair<int,int>> q;
        q.push({0,0});
        f[0][0] = 1;
        while(!q.empty()){
            auto [x,y] = q.front();
            q.pop();
            rep(i,4){
                if(x+dx[i]<0||x+dx[i]>=2*h-1||y+dy[i]<0||y+dy[i]>=2*w-1) continue;
                if(f[x+dx[i]][y+dy[i]]) continue;
                int nx=x+dx[i]*2, ny=y+dy[i]*2;
                if(chmin(f[nx][ny], f[x][y]+1)){
                    q.push({nx,ny});
                }
            }
        }
        if(f[2*h-2][2*w-2]==1e9) cout << 0 << endl;
        else cout << f[2*h-2][2*w-2] << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 8