Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

black = []
white = []

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            black.append((i, j))
        else:
            white.append((i, j))

def check(u, v, p):
    now = p
    for x, y in box:
        if u + x < 0 or u + x >= h or v + y < 0 or v + y >= w:
            continue
        if s[u][v] == "." and s[u+x][v+y] == "#" and p[u+x][v+y] == 0:
            p[u+x][v+y] = 1
            now = check(u+x, v+y, p)

        elif s[u][v] == "#" and s[u+x][v+y] == "." and p[u+x][v+y] == 0:
            p[u+x][v+y] = 1
            now = check(u+x, v+y, p)
    return now

ans = 0
for i, j in black:
    p = [[0]*w for _ in range(h)]
    p[i][j] = 1
    t = check(i, j, p)
    for a, b in white:
        if t[a][b] == 1:
            ans += 1

print(ans)



Fixed submission:
import sys
sys.setrecursionlimit(10**6)

h, w = map(int, input().split())
s = [input() for _ in range(h)]
t = [[None]*w for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dfs(i, j, color):
    if not (0<=i<h and 0<=j<w): return 0, 0
    if s[i][j] == color: return 0, 0
    if t[i][j]: return 0, 0

    t[i][j] = True
    wh, bl = 0, 0
    if s[i][j] == "#":
        bl += 1
    else:
        wh += 1

    for x, y in box:
        tmp1, tmp2 = dfs(i+x, j+y, s[i][j])
        wh, bl = wh+tmp1, bl+tmp2

    return wh, bl

ans = 0
for i in range(h):
    for j in range(w):
        a, b = dfs(i, j, "!")
        ans += a*b
print(ans)

Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

black = []
white = []

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            black.append((i, j))
        else:
            white.append((i, j))

def check(u, v, p):
        for x, y in box:
            if u + x < 0 or u + x >= h or v + y < 0 or v + y >= w:
                continue
            if s[u][v] == "." and s[u+x][v+y] == "#" and p[u+x][v+y] == 0:
                p[u+x][v+y] = 1
                check(u+x, v+y, p)

            elif s[u][v] == "#" and s[u+x][v+y] == "." and p[u+x][v+y] == 0:
                p[u+x][v+y] = 1
                check(u+x, v+y, p)
        return p

ans = 0
for i, j in black:
    p = [[0]*w for _ in range(h)]
    p[i][j] = 1
    t = check(i, j, p)
    for a, b in white:
        if t[a][b] == 1:
            ans += 1

print(ans)



Fixed submission:
import sys
sys.setrecursionlimit(10**6)

h, w = map(int, input().split())
s = [input() for _ in range(h)]
t = [[None]*w for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dfs(i, j, color):
    if not (0<=i<h and 0<=j<w): return 0, 0
    if s[i][j] == color: return 0, 0
    if t[i][j]: return 0, 0

    t[i][j] = True
    wh, bl = 0, 0
    if s[i][j] == "#":
        bl += 1
    else:
        wh += 1

    for x, y in box:
        tmp1, tmp2 = dfs(i+x, j+y, s[i][j])
        wh, bl = wh+tmp1, bl+tmp2

    return wh, bl

ans = 0
for i in range(h):
    for j in range(w):
        a, b = dfs(i, j, "!")
        ans += a*b
print(ans)

Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

black = []
white = []

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            black.append((i, j))
        else:
            white.append((i, j))

def check(u, v, p):
        for x, y in box:
            if u + x < 0 or u + x >= h or v + y < 0 or v + y >= w:
                continue
            if s[u][v] == "." and s[u+x][v+y] == "#" and p[u+x][v+y] == 0:
                p[u+x][v+y] = 1
                check(u+x, v+y, p)

            elif s[u][v] == "#" and s[u+x][v+y] == "." and p[u+x][v+y] == 0:
                p[u+x][v+y] = 1
                check(u+x, v+y, p)
        return p

ans = 0
for i, j in black:
    p = [[0]*w for _ in range(h)]
    p[i][j] = 1
    for a, b in white:
        t = check(i, j, p)
        if p[a][b] == 1:
            ans += 1

print(ans)



Fixed submission:
import sys
sys.setrecursionlimit(10**6)

h, w = map(int, input().split())
s = [input() for _ in range(h)]
t = [[None]*w for _ in range(h)]
box = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dfs(i, j, color):
    if not (0<=i<h and 0<=j<w): return 0, 0
    if s[i][j] == color: return 0, 0
    if t[i][j]: return 0, 0

    t[i][j] = True
    wh, bl = 0, 0
    if s[i][j] == "#":
        bl += 1
    else:
        wh += 1

    for x, y in box:
        tmp1, tmp2 = dfs(i+x, j+y, s[i][j])
        wh, bl = wh+tmp1, bl+tmp2

    return wh, bl

ans = 0
for i in range(h):
    for j in range(w):
        a, b = dfs(i, j, "!")
        ans += a*b
print(ans)