Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)
h,w=[int(j) for j in input().split()]
s=[list(input())for i in range(h)]
ans=0
def f(i,j,se):
    se.add((i,j))
    res=0
    b=True
    for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
        p,q=i+x,j+y
        if 0<=p<h and 0<=q<w:
            if s[p][q]!=s[i][j] and (p,q) not in se:
                b=False
                res+=f(p,q,se)
    if b:
        if s[i][j]==".":
            return 1
        else:
            return 0
    else:
        if s[i][j]==".":
            return res+1
        else:
            return res

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            ans+=f(i,j,set())

print(ans)

Fixed submission:
h,w=[int(j) for j in input().split()]
s=[input()for i in range(h)]
par=[-1 for i in range(h*w)]    #par<0なら自身が親で要素の個数、>0なら子で親の位置示す
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])

def size(a):
    return -par[root(a)]

def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(h):
    for j in range(w):
        for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
            p,q=i+x,j+y
            if 0<=p<h and 0<=q<w:
                if s[p][q]!=s[i][j]:
                    if root(w*p+q)!=root(w*i+j):
                        connect(w*p+q,w*i+j)

from collections import Counter
c=Counter()
for i in range(h):
    for j in range(w):
        p=i*w+j
        q=root(p)
        if c[q]==0:
            c[q]=[0,0]
        if s[i][j]=="#":
            c[q][0]+=1
        else:
            c[q][1]+=1
ans=0
for i in c:
    ans+=c[i][0]*c[i][1]
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w=[int(j) for j in input().split()]
s=[input()for i in range(h)]
ans=0
def f(i,j,se):
    se.add((i,j))
    res=0
    b=True
    for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
        p,q=i+x,j+y
        if 0<=p<h and 0<=q<w:
            if s[p][q]!=s[i][j] and (p,q) not in se:
                b=False
                res+=f(p,q,se)
    if b:
        if s[i][j]==".":
            return 1
        else:
            return 0
    else:
        if s[i][j]==".":
            return res+1
        else:
            return res

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            ans+=f(i,j,set())

print(ans)

Fixed submission:
h,w=[int(j) for j in input().split()]
s=[input()for i in range(h)]
par=[-1 for i in range(h*w)]    #par<0なら自身が親で要素の個数、>0なら子で親の位置示す
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])

def size(a):
    return -par[root(a)]

def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(h):
    for j in range(w):
        for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
            p,q=i+x,j+y
            if 0<=p<h and 0<=q<w:
                if s[p][q]!=s[i][j]:
                    if root(w*p+q)!=root(w*i+j):
                        connect(w*p+q,w*i+j)

from collections import Counter
c=Counter()
for i in range(h):
    for j in range(w):
        p=i*w+j
        q=root(p)
        if c[q]==0:
            c[q]=[0,0]
        if s[i][j]=="#":
            c[q][0]+=1
        else:
            c[q][1]+=1
ans=0
for i in c:
    ans+=c[i][0]*c[i][1]
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w=[int(j) for j in input().split()]
s=[input()for i in range(h)]
ans=0
def f(i,j,se):
    se.add((i,j))
    res=0
    b=True
    for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
        p,q=i+x,j+y
        if 0<=p<h and 0<=q<w:
            if s[p][q]!=s[i][j] and (p,q) not in se:
                b=False
                res+=f(p,q,se)
    if b:
        if s[i][j]==".":
            return 1
        else:
            return 0
    else:
        if s[i][j]==".":
            return res+1
        else:
            return res

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            ans+=f(i,j,set())

print(ans)

Fixed submission:
h,w=[int(j) for j in input().split()]
s=[input()for i in range(h)]
par=[-1 for i in range(h*w)]    #par<0なら自身が親で要素の個数、>0なら子で親の位置示す
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])

def size(a):
    return -par[root(a)]

def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(h):
    for j in range(w):
        for x,y in [(1,0),(-1,0),(0,1),(0,-1)]:
            p,q=i+x,j+y
            if 0<=p<h and 0<=q<w:
                if s[p][q]!=s[i][j]:
                    if root(w*p+q)!=root(w*i+j):
                        connect(w*p+q,w*i+j)

from collections import Counter
c=Counter()
for i in range(h):
    for j in range(w):
        p=i*w+j
        q=root(p)
        if c[q]==0:
            c[q]=[0,0]
        if s[i][j]=="#":
            c[q][0]+=1
        else:
            c[q][1]+=1
ans=0
for i in c:
    ans+=c[i][0]*c[i][1]
print(ans)