Problem p02577 (python)

Fixed submission:
# coding: utf-8
import functools
n = list(map(int, list(input())))

def add(a, b):
    return a+b

if functools.reduce(add, n, 0) % 9 == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
# coding: utf-8
from functools import reduce
from queue import Queue

H, W = list(map(int, input().split(" ")))
current_h, current_w = list(map(int, input().split(" ")))
current_h -= 1
current_w -= 1
goal_h, goal_w = list(map(int, input().split(" ")))
goal_h -= 1
goal_w -= 1

table = [list(input()) for _ in range(H)]


class DFS:

    def __init__(self, table, current_h, current_w):
        self.table = table
        self.current_h = current_h
        self.current_w = current_w
        self.islands = []
        self.start_island_index = -1
        self.end_island_index = -1

    def delta(self, h, w):
        return [(h+1, w), (h, w-1), (h, w), (h, w+1), (h-1, w)]

    def dfs(self, start_h, start_w):
        next = self.delta(start_h, start_w)
        island = [(start_h, start_w)]
        if start_h == self.current_h and start_w == self.current_w:
            self.start_island_index = len(self.islands)

        for h, w in next:
            if h == self.current_h and w == self.current_w:
                self.start_island_index = len(self.islands)
            if h == goal_h and w == goal_w:
                self.end_island_index = len(self.islands)
            if not(0 <= h < H and 0 <= w < W):
                continue
            elif self.table[h][w] == "#":
                continue
            else:
                island.append((h, w))
                self.table[h][w] = "#"
                next.extend(self.delta(h, w))
        self.islands.append(island)

    def make_islands(self):
        for h in range(H):
            for w in range(W):
                if self.table[h][w] == ".":
                    self.dfs(h, w)

    def delta_five(self, pos):
        delta = []
        h = pos[0]
        w = pos[1]
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                if dh == 0 and dw == 0:
                    continue
                delta.append((h+dh, w+dw))
        return delta

    def make_islands_graph(self):
        islands_graph = []
        for start_island_index in range(len(self.islands)):
            for end_island_index in range(len(self.islands)):
                if start_island_index == end_island_index:
                    continue
                if self.is_connect(self.islands[start_island_index], self.islands[end_island_index]):
                    islands_graph.append((start_island_index, end_island_index))
        return islands_graph

    def is_connect(self, islands_a, islands_b):
        a = set(reduce(lambda a, b: a + b, map(self.delta_five, islands_a)))
        for h, w in islands_b:
            if (h, w) in a:
                return True
        return False

class BFS:

    def __init__(self, length_of_islands, graph, start_island_index, end_island_index):
        self.islands = [0 for i in range(0, length_of_islands)]
        self.graph = graph
        self.start = start_island_index
        self.end = end_island_index

    def bfs(self, start, end):
        q = Queue()
        q.put(start)
        count = 0
        while not q.empty():
            now = q.get()

            if now == end:
                return count
            if self.islands[now] == -1:
                continue
            for s, e in self.graph:
                if self.islands[s] == -1:
                    continue
                if s == now:
                    q.put(e)
            self.islands[now] = -1
            count += 1
        return -1

dfs = DFS(table, current_h, current_w)
dfs.make_islands()
graph = dfs.make_islands_graph()
# bfs = BFS(len(dfs.islands), graph, dfs.start_island_index, dfs.end_island_index)
# print(bfs.bfs(dfs.start_island_index, dfs.end_island_index))