Problem p02855 (python)

Fixed submission:
def main():
    H, W, K = (int(i) for i in input().split())
    c = [input() for j in range(H)]
    from collections import deque
    st = deque()
    dist = [[-1]*W for i in range(H)]
    berry = 0
    for h in range(H):
        for w in range(W):
            if c[h][w] == '#':
                berry += 1
                st.append((h, w, berry))
                dist[h][w] = 0
    result = [[0]*W for _ in range(H)]

    def bfs():
        que = st
        d = ((0, 1), (0, -1))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    def bfs2():
        que = st
        d = ((1, 0), (-1, 0))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    bfs()
    allzero_flag = True
    for i in range(H):
        if result[i][0] != 0:
            allzero_flag = False
    if allzero_flag:
        st = deque()
        dist = [[-1]*W for i in range(H)]
        berry = 0
        for h in range(H):
            for w in range(W):
                if c[h][w] == '#':
                    berry += 1
                    st.append((h, w, berry))
                    dist[h][w] = 0
        result = [[0]*W for _ in range(H)]
        bfs2()
        for h in range(H):
            for j in range(W):
                cur = j
                if result[h][w] != 0:
                    print(result[h][w], end=" ")
                elif w < W - 1:
                    while result[h][cur] == 0:
                        cur += 1
                    print(result[h][cur], end=" ")
                else:
                    while result[h][cur] == 0:
                        cur -= 1
                    print(result[h][cur], end=" ")
            print()
    else:
        for i, r in enumerate(result):
            cur = i
            flag = True
            if i < H - 1 and result[i][0] == 0:
                while flag and result[cur][0] == 0:
                    cur += 1
                    if cur == H:
                        cur = i
                        while result[cur][0] == 0:
                            cur -= 1
                        flag = False
                print(*result[cur])
            elif 0 < i and result[i][0] == 0:
                while flag and result[cur][0] == 0:
                    cur -= 1
                    if cur == -1:
                        cur = i
                        while result[cur][0] == 0:
                            cur += 1
                        flag = False
                print(*result[cur])
            else:
                print(*r)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H, W, K = (int(i) for i in input().split())
    c = [input() for j in range(H)]
    from collections import deque
    st = deque()
    dist = [[-1]*W for i in range(H)]
    berry = 0
    for h in range(H):
        for w in range(W):
            if c[h][w] == '#':
                berry += 1
                st.append((h, w, berry))
                dist[h][w] = 0
    result = [[0]*W for _ in range(H)]

    def bfs():
        que = st
        d = ((0, 1), (0, -1))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    def bfs2():
        que = st
        d = ((1, 0), (-1, 0))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    bfs()
    allzero_flag = True
    for i in range(H):
        if result[i][0] != 0:
            allzero_flag = False
    if allzero_flag:
        st = deque()
        dist = [[-1]*W for i in range(H)]
        berry = 0
        for h in range(H):
            for w in range(W):
                if c[h][w] == '#':
                    berry += 1
                    st.append((h, w, berry))
                    dist[h][w] = 0
        result = [[0]*W for _ in range(H)]
        bfs2()
        for h in range(H):
            for j in range(W):
                cur = j
                if result[h][w] != 0:
                    print(result[h][w], end=" ")
                elif w < W - 1:
                    while result[h][cur] == 0:
                        cur += 1
                    print(result[h][cur], end=" ")
                else:
                    while result[h][cur] == 0:
                        cur -= 1
                    print(result[h][cur], end=" ")
            print()
    else:
        for i, r in enumerate(result):
            cur = i
            if i < H - 1 and result[i][0] == 0:
                while result[cur][0] == 0:
                    cur += 1
                print(*result[cur])
            elif 0 < i and result[i][0] == 0:
                while result[cur][0] == 0:
                    cur -= 1
                print(*result[i-1])
            else:
                print(*r)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H, W, K = (int(i) for i in input().split())
    c = [input() for j in range(H)]
    from collections import deque
    st = deque()
    dist = [[-1]*W for i in range(H)]
    berry = 0
    for h in range(H):
        for w in range(W):
            if c[h][w] == '#':
                berry += 1
                st.append((h, w, berry))
                dist[h][w] = 0
    result = [[0]*W for _ in range(H)]

    def bfs():
        que = st
        d = ((0, 1), (0, -1))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    def bfs2():
        que = st
        d = ((1, 0), (-1, 0))
        while que:
            *u, b = que.popleft()
            result[u[0]][u[1]] = b
            for dy, dx in d:
                next_y = u[0] + dy
                next_x = u[1] + dx
                if not(0 <= next_y < H and 0 <= next_x < W):
                    continue
                if dist[next_y][next_x] != -1:
                    continue
                if c[next_y][next_x] == '#':
                    continue
                dist[next_y][next_x] = dist[u[0]][u[1]] + 1
                result[next_y][next_x] = b
                que.append((next_y, next_x, b))

    bfs()
    allzero_flag = True
    for i in range(H):
        if result[i][0] != 0:
            allzero_flag = False
    if allzero_flag:
        result = [[0]*W for _ in range(H)]
        bfs2()
        for h in range(H):
            for j in range(W):
                cur = j
                if result[h][w] != 0:
                    print(result[h][w], end=" ")
                elif w < W - 1:
                    while result[h][cur] == 0:
                        cur += 1
                    print(result[h][cur], end=" ")
                else:
                    while result[h][cur] == 0:
                        cur -= 1
                    print(result[h][cur], end=" ")
            print()
    else:
        for i, r in enumerate(result):
            cur = i
            if i < H - 1 and result[i][0] == 0:
                while result[cur][0] == 0:
                    cur += 1
                print(*result[cur])
            elif 0 < i and result[i][0] == 0:
                while result[cur][0] == 0:
                    cur -= 1
                print(*result[i-1])
            else:
                print(*r)


if __name__ == '__main__':
    main()