Buggy python submission (verdict: Runtime Error):
from collections import deque
while True:
    W,H = map(int, input().split())
    if W == 0: break
    es = [[] for i in range(W*H)]
    for y in range(H):
        walls_h = map(int, input().split())
        for x in range(W-1):
            if walls_h[x]: continue
            es[y*W + x].append(y*W + x+1)
            es[y*W + x+1].append(y*W + x)
        if y == H-1: break
        walls_v = map(int, input().split())
        for x in range(W):
            if walls_v[x]: continue
            es[y*W + x].append((y+1)*W + x)
            es[(y+1)*W + x].append(y*W + x)
    q = deque([0])
    dist = [0] * (W*H)
    dist[0] = 1
    while q:
        fr = q.popleft()
        for to in es[fr]:
            if dist[to] == 0:
                dist[to] = dist[fr] + 1
                q.append(to)
    print(dist[-1])

Fixed submission:
from collections import deque
while True:
    W,H = map(int, input().split())
    if W == 0: break
    es = [[] for i in range(W*H)]
    for y in range(H):
        walls_h = list(map(int, input().split()))
        for x in range(W-1):
            if walls_h[x]: continue
            es[y*W + x].append(y*W + x+1)
            es[y*W + x+1].append(y*W + x)
        if y == H-1: break
        walls_v = list(map(int, input().split()))
        for x in range(W):
            if walls_v[x]: continue
            es[y*W + x].append((y+1)*W + x)
            es[(y+1)*W + x].append(y*W + x)
    q = deque([0])
    dist = [0] * (W*H)
    dist[0] = 1
    while q:
        fr = q.popleft()
        for to in es[fr]:
            if dist[to] == 0:
                dist[to] = dist[fr] + 1
                q.append(to)
    print(dist[-1])

Buggy python submission (verdict: Runtime Error):
from collections import deque
while True:
    W,H = map(int, input().split())
    if W == 0: break
    es = [[] for i in range(W*H)]
    for y in range(H):
        walls_h = map(int, input().split())
        for x in range(W-1):
            if walls_h[x]: continue
            es[y*W + x].append(y*W + x+1)
            es[y*W + x+1].append(y*W + x)
        if y == H-1: break
        walls_v = map(int, input().split())
        for x in range(W):
            if walls_v[x]: continue
            es[y*W + x].append((y+1)*W + x)
            es[(y+1)*W + x].append(y*W + x)
    q = deque()
    q.append(0)
    dist = [0] * (W*H)
    dist[0] = 1
    while q:
        fr = q.popleft()
        for to in es[fr]:
            if dist[to] == 0:
                dist[to] = dist[fr] + 1
                q.append(to)
    print(dist[-1])

Fixed submission:
from collections import deque
while True:
    W,H = map(int, input().split())
    if W == 0: break
    es = [[] for i in range(W*H)]
    for y in range(H):
        walls_h = list(map(int, input().split()))
        for x in range(W-1):
            if walls_h[x]: continue
            es[y*W + x].append(y*W + x+1)
            es[y*W + x+1].append(y*W + x)
        if y == H-1: break
        walls_v = list(map(int, input().split()))
        for x in range(W):
            if walls_v[x]: continue
            es[y*W + x].append((y+1)*W + x)
            es[(y+1)*W + x].append(y*W + x)
    q = deque([0])
    dist = [0] * (W*H)
    dist[0] = 1
    while q:
        fr = q.popleft()
        for to in es[fr]:
            if dist[to] == 0:
                dist[to] = dist[fr] + 1
                q.append(to)
    print(dist[-1])