Problem p02579 (python)

Fixed submission:
import sys
from collections import deque
from copy import copy

sys.setrecursionlimit(10**7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())

INF = 10**7


def main():
    H, W = in_nn()
    Ch, Cw = in_nn()
    Dh, Dw = in_nn()
    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1
    grid = in_map2(H)

    dp = [[INF for _ in range(W)] for _ in range(H)]
    dp[Ch][Cw] = 0

    q = deque()
    q.append((Cw, Ch))

    while q:
        now = q.popleft()
        x, y = now
        for px in range(-2, 3):
            for py in range(-2, 3):
                if (px == 0 and py == 0):
                    continue
                nx, ny = x + px, y + py
                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx]:
                    if (px == -1 and py == 0) or (px == 1 and py == 0) \
                            or (px == 0 and py == -1) or (px == 0 and py == 1):
                        if dp[ny][nx] > dp[y][x]:
                            dp[ny][nx] = dp[y][x]
                            q.appendleft((nx, ny))
                    else:
                        if dp[ny][nx] > dp[y][x] + 1:
                            dp[ny][nx] = dp[y][x] + 1
                            q.append((nx, ny))

    # for i in range(H):
    #     print(' '.join(map(str, dp[i])))

    if dp[Dh][Dw] >= INF:
        ans = -1
    else:
        ans = dp[Dh][Dw]

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W, M = in_nn()

    hcnt = [0] * H
    wcnt = [0] * W

    hw = []

    for i in range(M):
        h, w = in_nn()
        h, w = h - 1, w - 1
        hw.append((h, w))

    for i in range(M):
        h, w = hw[i]
        hcnt[h] += 1
        wcnt[w] += 1

    max_h = max(hcnt)
    max_w = max(wcnt)

    ans = max_h + max_w - 1

    h_index = []
    w_index = []

    for i in range(H):
        if hcnt[i] == max_h:
            h_index.append(i)

    for i in range(W):
        if wcnt[i] == max_w:
            w_index.append(i)

    hw_set = set(hw)
    # print(h_index)
    # print(w_index)

    hn = len(h_index)
    wn = len(w_index)

    f = False
    for i in range(hn):
        for j in range(wn):
            if (h_index[i], w_index[j]) not in hw_set:
                f = True

        if f or hn * i > 10**7:
            ans += 1
            break

    print(ans)


if __name__ == '__main__':
    main()