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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

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

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y, isVisited ):
    ans = 0
    isVisited[x][y] = True
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if S[x][y] == '#':
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '.': ans += dfs( nx, ny, isVisited )+1
        else:
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '#': ans += dfs( nx, ny, isVisited )
    return ans


ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
          isVisited = [ [False]*W for _ in range(H) ]
          ans += dfs( i, j, isVisited )

print( ans )


Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

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

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y, isVisited ):
    ans = 0
    isVisited[x][y] = True
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if S[x][y] == '#':
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '.': ans += dfs( nx, ny, isVisited )+1
        else:
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '#': ans += dfs( nx, ny, isVisited )
    return ans


ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
          isVisited = [ [False]*W for _ in range(H) ]
          ans += dfs( i, j, isVisited )

print( ans )


Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

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

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y, isVisited ):
    ans = 0
    isVisited[x][y] = True
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if S[x][y] == '#':
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '.': ans += dfs( nx, ny, isVisited )+1
        else:
            if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
                if S[nx][ny] == '#': ans += dfs( nx, ny, isVisited )
    return ans


ans = 0
for i in range(H):
    for j in range(W):
        isVisited = [ [False]*W for _ in range(H) ]
        if S[i][j] == '#': ans += dfs( i, j, isVisited )

print( ans )


Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

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


# (i, j) から行けるペアの数
def dfs( i, j, isVisited ):
    ans = 0
    isVisited[i][j] = True
    if S[i][j] == '#':
        if i+1 < H and isVisited[i+1][j] == False:
            if S[i+1][j] == '.': ans += dfs( i+1, j, isVisited )+1
        if j+1 < W and isVisited[i][j+1] == False:
            if S[i][j+1] == '.': ans += dfs( i, j+1, isVisited )+1
        if i-1 >= 0 and isVisited[i-1][j] == False:
            if S[i-1][j] == '.': ans += dfs( i-1, j, isVisited )+1 
        if j-1 >= 0 and isVisited[i][j-1] == False:
            if S[i][j-1] == '.': ans += dfs( i, j-1, isVisited )+1
    else:
        if i+1 < H and isVisited[i+1][j] == False:
            if S[i+1][j] == '#': ans += dfs( i+1, j, isVisited )
        if j+1 < W and isVisited[i][j+1] == False:
            if S[i][j+1] == '#': ans += dfs( i, j+1, isVisited )
        if i-1 >= 0 and isVisited[i-1][j] == False:
            if S[i-1][j] == '#': ans += dfs( i-1, j, isVisited ) 
        if j-1 >= 0 and isVisited[i][j-1] == False:
            if S[i][j-1] == '#': ans += dfs( i, j-1, isVisited )
    return ans


ans = 0
for i in range(H):
    for j in range(W):
        isVisited = [ [False]*W for _ in range(H) ]
        if S[i][j] == '#': ans += dfs( i, j, isVisited )

print( ans )


Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )

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


# (i, j) から行けるペアの数
def dfs( i, j, isVisited ):
    ans = 0
    isVisited[i][j] = True
    if S[i][j] == '#':
        if i+1 < H and isVisited[i+1][j] == False:
            if S[i+1][j] == '.': ans += dfs( i+1, j, isVisited )+1
        if j+1 < W and isVisited[i][j+1] == False:
            if S[i][j+1] == '.': ans += dfs( i, j+1, isVisited )+1
        if i-1 >= 0 and isVisited[i-1][j] == False:
            if S[i-1][j] == '.': ans += dfs( i-1, j, isVisited )+1 
        if j-1 >= 0 and isVisited[i][j-1] == False:
            if S[i][j-1] == '.': ans += dfs( i, j-1, isVisited )+1
    else:
        if i+1 < H and isVisited[i+1][j] == False:
            if S[i+1][j] == '#': ans += dfs( i+1, j, isVisited )
        if j+1 < W and isVisited[i][j+1] == False:
            if S[i][j+1] == '#': ans += dfs( i, j+1, isVisited )
        if i-1 >= 0 and isVisited[i-1][j] == False:
            if S[i-1][j] == '#': ans += dfs( i-1, j, isVisited ) 
        if j-1 >= 0 and isVisited[i][j-1] == False:
            if S[i][j-1] == '#': ans += dfs( i, j-1, isVisited )
    return ans


ans = 0
for i in range(H):
    for j in range(W):
        isVisited = [ [False]*W for _ in range(H) ]
        if S[i][j] == '#': ans += dfs( i, j, isVisited )

print( ans )


Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(200000)

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


isVisited = [ [False]*W for _ in range(H) ]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
# (i, j) から行けるペアの数
def dfs( x, y ):
    isVisited[x][y] = True
    numBlack, numWhite = 0, 0
    if S[x][y] == '#': numBlack = 1
    else:              numWhite = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx and nx < H and 0 <= ny and ny < W and not isVisited[nx][ny]:
            if S[x][y] != S[nx][ny]:
                tmpB, tmpW = dfs( nx, ny )
                numBlack += tmpB
                numWhite += tmpW
    return numBlack, numWhite

ans = 0
for i in range(H):
    for j in range(W):
        if not isVisited[i][j]: 
            numBlack, numWhite = dfs( i, j )
            ans += numBlack * numWhite

print( ans )