Problem p03436 (python)

Fixed submission:
def main():
    H, W = [int(x) for x in input().strip().split()]
    tables = []
    white_count = 0
    for i in range(H):
        rows = [str(x) for x in list(input().strip())]
        white_count += rows.count(".")
        tables.append(rows)

    next_cells = [(0, 0, 1)]
    while(next_cells):
        _next_cells = []
        for next_cell in next_cells:
            y, x, step = next_cell
            if y == H-1 and x == W-1: #Goal
                tables[y][x] = str(min(step+1, int(tables[y][x])))
                continue

            if y != 0: # up OK
                if tables[y-1][x] != "#": # move OK
                    if tables[y-1][x] != ".": # 既に通った
                        if step+1 < int(tables[y-1][x]):
                            tables[y-1][x] = str(step+1)
                            _next_cells.append((y-1, x, step+1))
                    else: # 初めて通る
                        tables[y-1][x] = str(step+1)
                        _next_cells.append((y-1, x, step+1))

            if y != H-1: # down OK
                if tables[y+1][x] != "#": # move OK
                    if tables[y+1][x] != ".": # 既に通った
                        if step+1 < int(tables[y+1][x]):
                            tables[y+1][x] = str(step+1)
                            _next_cells.append((y+1, x, step+1))
                    else: # 初めて通る
                        tables[y+1][x] = str(step+1)
                        _next_cells.append((y+1, x, step+1))

            if x != 0: # left OK
                if tables[y][x-1] != "#": # move OK
                    if tables[y][x-1] != ".": # 既に通った
                        if step+1 < int(tables[y][x-1]):
                            tables[y][x-1] = str(step+1)
                            _next_cells.append((y, x-1, step+1))
                    else: # 初めて通る
                        tables[y][x-1] = str(step+1)
                        _next_cells.append((y, x-1, step+1))

            if x != W-1: # right OK
                if tables[y][x+1] != "#": # move OK
                    if tables[y][x+1] != ".": # 既に通った
                        if step+1 < int(tables[y][x+1]):
                            tables[y][x+1] = str(step+1)
                            _next_cells.append((y, x+1, step+1))
                    else: # 初めて通る
                        tables[y][x+1] = str(step+1)
                        _next_cells.append((y, x+1, step+1))
        # end for
        next_cells = list(set(_next_cells)) # 重複カット
    # end while
    if tables[H-1][W-1] == ".":
        return -1
    return white_count - int(tables[H-1][W-1])
    
if __name__ == '__main__':
    print(main())

Buggy submission (verdict: Runtime Error):
def main():
    H, W = [int(x) for x in input().strip().split()]
    tables = []
    white_count = 0
    for i in range(H):
        rows = [str(x) for x in list(input().strip())]
        white_count += rows.count(".")
        tables.append(rows)

    next_cells = [(0, 0, 1)]
    while(next_cells):
        _next_cells = []
        for next_cell in next_cells:
            y, x, step = next_cell
            if y == H-1 and x == W-1: #Goal
                tables[y][x] = str(min(step+1, int(tables[y][x])))
                continue

            for cond, ys, xs in [(y != 0,y-1,x),(y != H-1,y+1,x),(x != 0,y,x-1),(x != W-1,y,x+1)]
                if cond: # up OK
                    if tables[ys][xs] != "#": # move OK
                        if tables[ys][xs] != ".": # 既に通った
                            if step+1 < int(tables[ys][xs]):
                                tables[ys][xs] = str(step+1)
                                _next_cells.append((ys, xs, step+1))
                        else: # 初めて通る
                            tables[ys][xs] = str(step+1)
                            _next_cells.append((ys, xs, step+1))
            # end for
        # end for
        next_cells = list(set(_next_cells)) # 重複カット
    # end while
    if tables[H-1][W-1] == ".":
        return -1
    return white_count - int(tables[H-1][W-1])
    
if __name__ == '__main__':
    print(main())