Problem p03157 (python)

Fixed submission:
import sys
sys.setrecursionlimit(400*400*400)

H, W = map(int, input().split())
S = [input() for i in range(H)]

mask = ["".join(["#" if (i + j) % 2 else "." for j in range(W)]) for i in range(H)]

masked_S = [[1 if S[i][j] == mask[i][j] else 0 for j in range(W)] for i in range(H)]

S_colored = [[-1 for j in range(W)] for i in range(H)]

color_count = dict()


def fill(i, j, color):
    S_colored[i][j] = color

    if S[i][j] == "#":
        color_count[color][0] += 1
    else:
        color_count[color][1] += 1

    for yx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
        dy, dx = yx
        i2, j2 = i + dy, j + dx

        if i2 < 0 or i2 >= H or j2 < 0 or j2 >= W:
            continue

        if S_colored[i2][j2] != -1:
            continue

        if masked_S[i2][j2] != masked_S[i][j]:
            continue

        fill(i2, j2, color)


def main():
    color = -1
    for i in range(H):
        for j in range(W):
            if S_colored[i][j] != -1:
                continue
            color += 1
            color_count[color] = [0, 0]
            fill(i, j, color)

    result = 0
    for k in color_count.keys():
        result += color_count[k][0] * color_count[k][1]

    print(result)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400*400*400*400)

H, W = map(int, input().split())
S = [input() for i in range(H)]

mask = ["".join(["#" if (i + j) % 2 else "." for j in range(W)]) for i in range(H)]

masked_S = [[1 if S[i][j] == mask[i][j] else 0 for j in range(W)] for i in range(H)]

S_colored = [[-1 for j in range(W)] for i in range(H)]

color_count = dict()


def fill(i, j, color):
    S_colored[i][j] = color

    if S[i][j] == "#":
        color_count[color][0] += 1
    else:
        color_count[color][1] += 1

    for yx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
        dy, dx = yx
        i2, j2 = i + dy, j + dx

        if i2 < 0 or i2 >= H or j2 < 0 or j2 >= W:
            continue

        if S_colored[i2][j2] != -1:
            continue

        if masked_S[i2][j2] != masked_S[i][j]:
            continue

        fill(i2, j2, color)


def main():
    color = -1
    for i in range(H):
        for j in range(W):
            if S_colored[i][j] != -1:
                continue
            color += 1
            color_count[color] = [0, 0]
            fill(i, j, color)

    result = 0
    for k in color_count.keys():
        result += color_count[k][0] * color_count[k][1]

    print(result)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400*400)

H, W = map(int, input().split())
S = [input() for i in range(H)]

mask = ["".join(["#" if (i + j) % 2 else "." for j in range(W)]) for i in range(H)]

masked_S = [[1 if S[i][j] == mask[i][j] else 0 for j in range(W)] for i in range(H)]

S_colored = [[-1 for j in range(W)] for i in range(H)]

color_count = dict()


def fill(i, j, color):
    S_colored[i][j] = color

    if S[i][j] == "#":
        color_count[color][0] += 1
    else:
        color_count[color][1] += 1

    for yx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
        dy, dx = yx
        i2, j2 = i + dy, j + dx

        if i2 < 0 or i2 >= H or j2 < 0 or j2 >= W:
            continue

        if S_colored[i2][j2] != -1:
            continue

        if masked_S[i2][j2] != masked_S[i][j]:
            continue

        fill(i2, j2, color)


def main():
    color = -1
    for i in range(H):
        for j in range(W):
            if S_colored[i][j] != -1:
                continue
            color += 1
            color_count[color] = [0, 0]
            fill(i, j, color)

    result = 0
    for k in color_count.keys():
        result += color_count[k][0] * color_count[k][1]

    print(result)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for i in range(H)]

mask = ["".join(["#" if (i + j) % 2 else "." for j in range(W)]) for i in range(H)]

masked_S = [[1 if S[i][j] == mask[i][j] else 0 for j in range(W)] for i in range(H)]

S_colored = [[-1 for j in range(W)] for i in range(H)]

color_count = dict()


def fill(i, j, color):
    S_colored[i][j] = color

    if S[i][j] == "#":
        color_count[color][0] += 1
    else:
        color_count[color][1] += 1

    for yx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
        dy, dx = yx
        i2, j2 = i + dy, j + dx

        if i2 < 0 or i2 >= H or j2 < 0 or j2 >= W:
            continue

        if S_colored[i2][j2] != -1:
            continue

        if masked_S[i2][j2] != masked_S[i][j]:
            continue

        fill(i2, j2, color)


def main():
    color = -1
    for i in range(H):
        for j in range(W):
            if S_colored[i][j] != -1:
                continue
            color += 1
            color_count[color] = [0, 0]
            fill(i, j, color)

    result = 0
    for k in color_count.keys():
        result += color_count[k][0] * color_count[k][1]

    print(result)

if __name__ == '__main__':
    main()