Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue as Qe

H,W = map(int,input().split())

D = [[-1 for j in range(W)] for i in range(H)]
S = [input() for i in range(H)]

D[0][0] = 1
q = Qe()
q.put((0,0))
White = 1 if S[0][0] == "." else 0
Black = 1 if S[0][0] == "#" else 0
ans = 0

def_h = 0
def_w = 0
while not q.empty():
    h,w = q.get()
    #print(h,w,White,Black)
    if h-1>=0 and S[h][w]!=S[h-1][w] and D[h-1][w] == -1: #up
        if S[h-1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h-1][w] = 1
        q.put((h-1,w))
    if h+1<H and S[h][w]!=S[h+1][w] and D[h+1][w] == -1: #down
        if S[h+1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h+1][w] = 1
        q.put((h+1,w))
    if w-1>=0 and S[h][w]!=S[h][w-1] and D[h][w-1] == -1: #left
        if S[h][w-1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w-1] = 1
        q.put((h,w-1))
    if w+1<W and S[h][w]!=S[h][w+1] and D[h][w+1] == -1: #right
        if S[h][w+1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w+1] = 1
        q.put((h,w+1))
    if q.empty():
        okei = False
        for i in range(def_h,H):
            for j in range(W):
                if D[i][j] == -1:
                    D[i][j] = 1
                    q.put((i,j))
                    White = 1 if S[i][j] == "." else 0
                    Black = 1 if S[i][j] == "#" else 0
                    def_h,def_w = i,j
                    okei = True
                    break
            if okei: break
                
print(ans)

Fixed submission:
from queue import Queue as Qe

H,W = map(int,input().split())

D = [[-1 for j in range(W)] for i in range(H)]
S = [input() for i in range(H)]

D[0][0] = 1
q = Qe()
q.put((0,0))
White = 1 if S[0][0] == "." else 0
Black = 1 if S[0][0] == "#" else 0
ans = 0

def_h = 0
def_w = 0
while not q.empty():
    h,w = q.get()
    #print(h,w,White,Black)
    if h-1>=0 and S[h][w]!=S[h-1][w] and D[h-1][w] == -1: #up
        if S[h-1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h-1][w] = 1
        q.put((h-1,w))
    if h+1<H and S[h][w]!=S[h+1][w] and D[h+1][w] == -1: #down
        if S[h+1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h+1][w] = 1
        q.put((h+1,w))
    if w-1>=0 and S[h][w]!=S[h][w-1] and D[h][w-1] == -1: #left
        if S[h][w-1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w-1] = 1
        q.put((h,w-1))
    if w+1<W and S[h][w]!=S[h][w+1] and D[h][w+1] == -1: #right
        if S[h][w+1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w+1] = 1
        q.put((h,w+1))
    if q.empty():
        okei = False
        for i in range(def_h,H):
            wara = def_w if i==def_h else 0
            for j in range(wara,W):
                if D[i][j] == -1:
                    D[i][j] = 1
                    q.put((i,j))
                    White = 1 if S[i][j] == "." else 0
                    Black = 1 if S[i][j] == "#" else 0
                    def_h,def_w = i,j
                    okei = True
                    break
            if okei: break
                
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue as Qe

H,W = map(int,input().split())

D = [[-1 for j in range(W)] for i in range(H)]
S = [input() for i in range(H)]

D[0][0] = 1
q = Qe()
q.put((0,0))
White = 1 if S[0][0] == "." else 0
Black = 1 if S[0][0] == "#" else 0
ans = 0

def_h = 0
def_w = 0
while not q.empty():
    h,w = q.get()
    if h-1>=0 and S[h][w]!=S[h-1][w] and D[h-1][w] == -1: #up
        if S[h-1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h-1][w] = 1
        q.put((h-1,w))
    if h+1<H and S[h][w]!=S[h+1][w] and D[h+1][w] == -1: #down
        if S[h+1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h+1][w] = 1
        q.put((h+1,w))
    if w-1>=0 and S[h][w]!=S[h][w-1] and D[h][w-1] == -1: #left
        if S[h][w-1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w-1] = 1
        q.put((h,w-1))
    if w+1<W and S[h][w]!=S[h][w+1] and D[h][w+1] == -1: #left
        if S[h][w+1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w+1] = 1
        q.put((h,w+1))
    if q.empty():
        okei = False
        for i in range(def_h,H):
            for j in range(def_w,W):
                if D[i][j] == -1:
                    D[i][j] = 1
                    q.put((i,j))
                    White = 1 if S[i][j] == "." else 0
                    Black = 1 if S[i][j] == "#" else 0
                    okei = True
                    break
            if okei: break
                
print(ans)

Fixed submission:
from queue import Queue as Qe

H,W = map(int,input().split())

D = [[-1 for j in range(W)] for i in range(H)]
S = [input() for i in range(H)]

D[0][0] = 1
q = Qe()
q.put((0,0))
White = 1 if S[0][0] == "." else 0
Black = 1 if S[0][0] == "#" else 0
ans = 0

def_h = 0
def_w = 0
while not q.empty():
    h,w = q.get()
    #print(h,w,White,Black)
    if h-1>=0 and S[h][w]!=S[h-1][w] and D[h-1][w] == -1: #up
        if S[h-1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h-1][w] = 1
        q.put((h-1,w))
    if h+1<H and S[h][w]!=S[h+1][w] and D[h+1][w] == -1: #down
        if S[h+1][w]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h+1][w] = 1
        q.put((h+1,w))
    if w-1>=0 and S[h][w]!=S[h][w-1] and D[h][w-1] == -1: #left
        if S[h][w-1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w-1] = 1
        q.put((h,w-1))
    if w+1<W and S[h][w]!=S[h][w+1] and D[h][w+1] == -1: #right
        if S[h][w+1]=="#":
            ans += White
            Black += 1
        else:
            ans += Black
            White += 1
        D[h][w+1] = 1
        q.put((h,w+1))
    if q.empty():
        okei = False
        for i in range(def_h,H):
            wara = def_w if i==def_h else 0
            for j in range(wara,W):
                if D[i][j] == -1:
                    D[i][j] = 1
                    q.put((i,j))
                    White = 1 if S[i][j] == "." else 0
                    Black = 1 if S[i][j] == "#" else 0
                    def_h,def_w = i,j
                    okei = True
                    break
            if okei: break
                
print(ans)