Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


ret=0
q = deque([]) 
def dfs(x,y,current):
	ret=0
	V=[ [ 0 for i in range(W) ] for j in range(H) ]
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						if next==".":  ret+=1
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return ret




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#":
			ret=0
			q = deque([]) 
			ans+=dfs(x,y, "#")

print ans



Fixed submission:
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


V=[ [ 0 for i in range(W) ] for j in range(H) ]


def dfs(x,y,current):
	bc=0	#black count
	wc=0	#white count
	q = deque([]) 
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		if current == "#":	  bc+=1
		else:  wc+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return bc*wc




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#" and V[x][y]==0:
			ans+=dfs(x,y, "#")

print ans

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


ret=0
q = deque([]) 


def dfs(x,y,current):
	ret=0
	V=[ [ 0 for i in range(W) ] for j in range(H) ]
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.pop()
		if S[x][y]==".":
			ret+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				if V[x+dx][y+dy]==0 and S[x+dx][y+dy] != current:
					if (x+dx, y+dy, S[x+dx][y+dy] ):
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy, S[x+dx][y+dy] ) )
	return ret



ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#":
			ret=0
			q = deque([]) 
			V=[ [ 0 for i in range(W) ] for j in range(H) ]
			ans+=dfs(x,y, "#")

print ans


Fixed submission:
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


V=[ [ 0 for i in range(W) ] for j in range(H) ]


def dfs(x,y,current):
	bc=0	#black count
	wc=0	#white count
	q = deque([]) 
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		if current == "#":	  bc+=1
		else:  wc+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return bc*wc




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#" and V[x][y]==0:
			ans+=dfs(x,y, "#")

print ans

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


ret=0
q = deque([]) 
V=[ [ 0 for i in range(W) ] for j in range(H) ]

def dfs(x,y,current):
	ret=0
	V[x][y]=1
	q.append( (x,y,current) )
	
	while q:
		x,y,current=q.pop()
		if S[x][y]==".":
			ret+=1
		V[x][y]=1
		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				if V[x+dx][y+dy]==0 and S[x+dx][y+dy] != current:
					if (x+dx, y+dy, S[x+dx][y+dy] ) not in q:
						q.append( (x+dx, y+dy, S[x+dx][y+dy] ) )	
	return ret



ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#":
			ret=0
			q = deque([]) 
			V=[ [ 0 for i in range(W) ] for j in range(H) ]
			ans+=dfs(x,y, "#")

print ans


Fixed submission:
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


V=[ [ 0 for i in range(W) ] for j in range(H) ]


def dfs(x,y,current):
	bc=0	#black count
	wc=0	#white count
	q = deque([]) 
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		if current == "#":	  bc+=1
		else:  wc+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return bc*wc




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#" and V[x][y]==0:
			ans+=dfs(x,y, "#")

print ans

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 
V=[ [ 0 for i in range(W) ] for j in range(H) ]

ret=0
def dfs(x,y,current):
	global ret
	V[x][y]=1
	if current==".":
		ret+=1
	else:
		ret+=0

	for dx,dy in D:
		if 0<=x+dx<H and 0<=y+dy<W:
			if V[x+dx][y+dy]==0:
				if S[x+dx][y+dy] != current:
					if current==".":
						dfs(x+dx,y+dy,"#")
					else:
						dfs(x+dx,y+dy,".")
	return ret


ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#":
			ret=0
			V=[ [ 0 for i in range(W) ] for j in range(H) ]
			ans+=dfs(x,y, "#")

print ans


Fixed submission:
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


V=[ [ 0 for i in range(W) ] for j in range(H) ]


def dfs(x,y,current):
	bc=0	#black count
	wc=0	#white count
	q = deque([]) 
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		if current == "#":	  bc+=1
		else:  wc+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return bc*wc




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#" and V[x][y]==0:
			ans+=dfs(x,y, "#")

print ans

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 
V=[ [ 0 for i in range(W) ] for j in range(H) ]

ret=0
def dfs(x,y,current):
	global ret
	V[x][y]=1
	if current==".":
		ret+=1
	else:
		ret+=0

	for dx,dy in D:
		if 0<=x+dx<H and 0<=y+dy<W:
			if V[x+dx][y+dy]==0:
				if S[x+dx][y+dy] != current:
					if current==".":
						dfs(x+dx,y+dy,"#")
					else:
						dfs(x+dx,y+dy,".")
	return ret


ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#":
			ret=0
			V=[ [ 0 for i in range(W) ] for j in range(H) ]
			ans+=dfs(x,y, "#")

print ans


Fixed submission:
from collections import deque

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


D=[ (1,0), (-1,0), (0,1), (0,-1) ] 


V=[ [ 0 for i in range(W) ] for j in range(H) ]


def dfs(x,y,current):
	bc=0	#black count
	wc=0	#white count
	q = deque([]) 
	V[x][y]=1
	q.append( (x,y,current) )

	while q:
		x,y,current=q.popleft()

		if current == "#":	  bc+=1
		else:  wc+=1

		for dx,dy in D:
			if 0<=x+dx<H and 0<=y+dy<W:
				next=S[x+dx][y+dy] 
				if V[x+dx][y+dy]==0 and current !=next:
						V[x+dx][y+dy]=1
						q.append( (x+dx, y+dy,next) )

	return bc*wc




ans=0
for x in range(H):
	for y in range(W):
		if S[x][y]=="#" and V[x][y]==0:
			ans+=dfs(x,y, "#")

print ans