Buggy python submission (verdict: Runtime Error):
# coding: utf-8

import sys
from collections import deque

if __name__ == '__main__':
    while True:
        m,n = map(int,input().split())
        sw = []
        bw = []
        if m == 0 and n == 0:
            break
        for i in range(2*n-1):
            if i % 2 == 0:
                sw.append(list(map(int,input().split())))
            else:
                bw.append(list(map(int,input().split())))
        flag = [[0 for i in range(m)] for j in range(n)]
        l = []
        l.append([0,0,1])
        res = 0
        while len(l) != 0:
            bf = l[0]
            l.pop(0)
            if bf[0] == n-1 and bf[1] == m-1:
                res = bf[2]
                break
            flag[bf[0]][bf[1]] = 1
            if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
                l.append([bf[0]-1,bf[1],bf[2]+1])
            if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
                l.append([bf[0],bf[1]+1,bf[2]+1])
            if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
                l.append([bf[0],bf[1]-1,bf[2]+1])
            if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
                l.append([bf[0]+1,bf[1],bf[2]+1])
        print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8

import sys
from collections import deque

if __name__ == '__main__':
    while True:
        m,n = map(int,input().split())
        sw = []
        bw = []
        if m == 0 and n == 0:
            break
        for i in range(2*n-1):
            if i % 2 == 0:
                sw.append(list(map(int,input().split())))
            else:
                bw.append(list(map(int,input().split())))
        flag = [[0 for i in range(m)] for j in range(n)]
        deq = deque()
        deq.append([0,0,1])
        res = 0
        while len(deq) != 0:
            bf = deq[0]
            deq.popleft()
            if bf[0] == n-1 and bf[1] == m-1:
                res = bf[2]
                break
            flag[bf[0]][bf[1]] = 1
            if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
                deq.append([bf[0]-1,bf[1],bf[2]+1])
            if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
                deq.append([bf[0],bf[1]+1,bf[2]+1])
            if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
                deq.append([bf[0],bf[1]-1,bf[2]+1])
            if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
                deq.append([bf[0]+1,bf[1],bf[2]+1])
        print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8

if __name__ == '__main__':
    while True:
        m,n = map(int,input().split())
        sw = []
        bw = []
        if m == 0 and n == 0:
            break
        for i in range(2*n-1):
            if i % 2 == 0:
                sw.append(list(map(int,input().split())))
            else:
                bw.append(list(map(int,input().split())))
        flag = [[0 for i in range(m)] for j in range(n)]
        l = [[0,0,1]]
        res = 0
        while l:
            bf = l[0]
            l.pop(0)
            if bf[0] == n-1 and bf[1] == m-1:
                res = bf[2]
                break
            flag[bf[0]][bf[1]] = 1
            if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
                l.append([bf[0]-1,bf[1],bf[2]+1])
            if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
                l.append([bf[0],bf[1]+1,bf[2]+1])
            if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
                l.append([bf[0],bf[1]-1,bf[2]+1])
            if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
                l.append([bf[0]+1,bf[1],bf[2]+1])
        print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [[0,0,1]]
    res = 0
    while l:
        bf = l[0]
        l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            l.append([bf[0]-1,bf[1],bf[2]+1])
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            l.append([bf[0],bf[1]+1,bf[2]+1])
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            l.append([bf[0],bf[1]-1,bf[2]+1])
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            l.append([bf[0]+1,bf[1],bf[2]+1])
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        x,y,z = bf[0],bf[1],bf[2]
        if x == n-1 and y == m-1:
            res = z
            break
        flag[x][y] = 1
        if 1 <= x and bw[x-1][y] == 0 and flag[x-1][y] == 0:
            l.append((x-1,y,z+1))
        if y < m-1 and sw[x][y] == 0 and flag[x][y+1] == 0:
            l.append((x,y+1,z+1))
        if 1 <= y and sw[x][y-1] == 0 and flag[x][y-1] == 0:
            l.append((x,y-1,z+1))
        if x < n-1 and bw[x][y] == 0 and flag[x+1][y] == 0:
            l.append((x+1,y,z+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        x,y,z = bf[0],bf[1],bf[2]
        if x == n-1 and y == m-1:
            res = z
            break
        flag[x][y] = 1
        if 1 <= x and bw[x-1][y] == 0 and flag[x-1][y] == 0:
            l.append((x-1,y,z+1))
        if y < m-1 and sw[x][y] == 0 and flag[x][y+1] == 0:
            l.append((x,y+1,z+1))
        if 1 <= y and sw[x][y-1] == 0 and flag[x][y-1] == 0:
            l.append((x,y-1,z+1))
        if x < n-1 and bw[x][y] == 0 and flag[x+1][y] == 0:
            l.append((x+1,y,z+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        x,y,z = bf[0],bf[1],bf[2]
        if x == n-1 and y == m-1:
            res = z
            break
        flag[x][y] += 1
        if 1 <= x and bw[x-1][y] == 0 and flag[x-1][y] == 0:
            l.append((x-1,y,z+1))
        if y < m-1 and sw[x][y] == 0 and flag[x][y+1] == 0:
            l.append((x,y+1,z+1))
        if 1 <= y and sw[x][y-1] == 0 and flag[x][y-1] == 0:
            l.append((x,y-1,z+1))
        if x < n-1 and bw[x][y] == 0 and flag[x+1][y] == 0:
            l.append((x+1,y,z+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)

Buggy python submission (verdict: Runtime Error):
while True:
    w, h = map(int, input().split())
    vart = []
    hori = []
    if w == 0 and h ==0:
        break
    for i in range(2*h-1):
        if i % 2 == 0:
            vart.append(list(map(int, input().split())))
        else:
            hori.append(list(map(int, input().split())))
    node = [[0 for i in range(h)] for i in range(w)]
    queue = [(0,0,1)]
    res = 0
    while queue:
        p = queue.pop(0)
        x, y, z = p[0], p[1], p[2]
        if x == w-1 and y == h-1:
            res = z
            break
        node[x][y] = 1
        if y-1 >= 0 and hori[y-1][x] == 0 and node[x][y-1] == 0:
            queue.append((x,y-1,z+1))
        if y+1 < h and hori[y][x] == 0 and node[x][y+1] == 0:
            queue.append((x,y+1,z+1))
        if x-1 >= 0 and vart[y][x-1] == 0 and node[x-1][y] == 0:
            queue.append((x-1,y,z+1))
        if x+1 < w and vart[y][x] == 0 and node[x+1][y] == 0:
            queue.append((x+1,y,z+1))
    print(res)

Fixed submission:
while True:
    m,n = map(int,input().split())
    sw = []
    bw = []
    if m == 0 and n == 0:
        break
    for i in range(2*n-1):
        if i % 2 == 0:
            sw.append(list(map(int,input().split())))
        else:
            bw.append(list(map(int,input().split())))
    flag = [[0 for i in range(m)] for j in range(n)]
    l = [(0,0,1)]
    res = 0
    while l:
        bf = l.pop(0)
        if bf[0] == n-1 and bf[1] == m-1:
            res = bf[2]
            break
        flag[bf[0]][bf[1]] = 1
        if 1 <= bf[0] and bw[bf[0]-1][bf[1]] == 0 and flag[bf[0]-1][bf[1]] == 0:
            flag[bf[0]-1][bf[1]] = 1
            l.append((bf[0]-1,bf[1],bf[2]+1))
        if bf[1] < m-1 and sw[bf[0]][bf[1]] == 0 and flag[bf[0]][bf[1]+1] == 0:
            flag[bf[0]][bf[1]+1] = 1
            l.append((bf[0],bf[1]+1,bf[2]+1))
        if 1 <= bf[1] and sw[bf[0]][bf[1]-1] == 0 and flag[bf[0]][bf[1]-1] == 0:
            flag[bf[0]][bf[1]-1] = 1
            l.append((bf[0],bf[1]-1,bf[2]+1))
        if bf[0] < n-1 and bw[bf[0]][bf[1]] == 0 and flag[bf[0]+1][bf[1]] == 0:
            flag[bf[0]+1][bf[1]] = 1
            l.append((bf[0]+1,bf[1],bf[2]+1))
    print(res)