Problem p02580 (python)

Fixed submission:
from collections import Counter
import itertools

def solve(ah, aw, s: set):
    ahh, ahc = ah
    aww, awc = aw
    if (ahh, aww) in s:
        return ahc+awc-1
    else:
        return ahc + awc

def main():
    H, W, M = map(int, input().split())
    LH = []
    LM = []
    s = set()
    for _ in range(M):
        h, w = map(int, input().split())
        LH.append(h)
        LM.append(w)
        s.add((h,w))
    
    ch = Counter(LH)
    cm = Counter(LM)

    ahp = ch.most_common()
    awp = cm.most_common()
    th = ahp[0][1]
    tw = awp[0][1]
    #print("t=", th + tw)
    ah = []
    for x in ahp:
        if x[1] == th:
            ah.append(x)
        else:
            break
    aw = []
    for x in awp:
        if x[1] == tw:
            aw.append(x)
        else:
            break
    ans = solve(ah[0], aw[0], s)
    for x, y in itertools.product(ah, aw):
        z = solve(x, y, s)
        if z > ans:
            print(z)
            exit()
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque
#import sys
#sys.setrecursionlimit(6000)

def dfs(start_h, start_w, S, H, W):
    path = deque()
    path.append((start_h, start_w))
    visited = set()
    #DFS
    while path:
        p = path.pop()
        if p in visited:
            continue
        visited.add(p)
        x, y = p
        for xx, yy in [(x,y+1), (x, y-1), (x+1, y), (x-1, y)]:
            if xx<0 or yy<0 or xx>H or yy>W:
                #print("Not exist:", xx, yy)
                continue
            #print(f"xx, yy={xx},{yy}")
            if S[yy][xx]:
                path.append((xx, yy))
    return visited

def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [[False]*(W+1)]  #S[w][h]でアクセス すなわちy,x
    for _ in range(W):
        S.append([False] + [True if x=='.' else False for x in input()])
    #DFS
    total_visited = set()
    visited = dfs(Ch, Cw, S, H, W)
    total_visited = total_visited.union(visited)
    if (Dh, Dw) in total_visited:
        print(0)
        exit()

    ans = 0
    while True:
        new_dist = set()
        for p in visited:
            x, y = p
            for xx, yy in [(x,y+2), (x, y-2), (x+2, y), (x-2, y)]:
                if xx<0 or yy<0 or xx>H or yy>W:
                    #print("Not exist:", xx, yy)
                    continue
                if (xx, yy) in total_visited:
                    continue
                if S[yy][xx]:
                    new_dist.add((xx, yy))
        #print(total_visited)
        #print(new_dist)
        kono = set()
        for dist in new_dist:
            x, y = dist
            if dist in total_visited:
                continue
            #print(f'new dist={x},{y}')
            new_visited = dfs(x, y, S, H, W)
            #print(f'new_visited={new_visited}')
            if new_visited:
                total_visited = total_visited.union(new_visited)
                ans += 1
                kono = kono.union(new_visited)
        if not kono:
            break
    if (Dh, Dw) in total_visited:
        print(ans)
    else:
        print(-1)




if __name__ == "__main__":
    main()