Problem:


Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6



Sample input:
3 3
.#.
..#
#..


Sample output:
10

Submission status: Runtime Error

Solution code:
import random

def search(search_map, group, current_h, current_w, group_index, prev):
    num = 0
    if not(current_h > -1 and current_w > -1 and current_h < H and current_w < W):
        return num
    if current_h in search_map:
        if current_w in search_map[current_h]:
            return num
    current = s_map[current_h][current_w]
    if current == prev:
        return num
    if current_h not in search_map:
        search_map[current_h] = {}
    search_map[current_h][current_w] = True

    if (current == '.'):
        num += 1
    # else:
    #    group[current_h][current_w] = group_index
    # 上
    num += search(search_map, group, current_h+1, current_w, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h-1, current_w, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h, current_w+1, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h, current_w-1, group_index, s_map[current_h][current_w])
    return num


def is_map_exist(m, h, w):
    if h in m:
        if w in m[h]:
            return True
    return False


'''
def nsin(X, n):
    if(int(X/n)):
        return nsin(int(X/n), n)+str(X % n)
    return str(X % n)


W = 2
H = 9
'''
H, W = map(int, input().split())
A = [input() for _ in range(H)]


'''
n = 2**(W*H)
for try_num in range(n):
    b = list(nsin(try_num, 2))
    A = []
    cnt = 0
    for j in range(H):
        s = ''
        for i in range(W):
            c = '.'
            if len(b) > cnt:
                if b[cnt] == '1':
                    c = '#'
            s += c
            cnt += 1
        A.append(s)
'''


s_map = {}
group = {}
searched_map = {}


for i in range(H):
    s_map[i] = {}
    group[i] = {}
    w = list(A[i])
    for j in range(W):
        s_map[i][j] = w[j]
        group[i][j] = 0

ans = 0
group_index = 1
group_cnt = {}
group_cnt[0] = 0
for i in range(H):
    for j in range(W):
        if s_map[i][j] == '#':
            if group[i][j] != 0:
                ans += group_cnt[group[i][j]]
            else:
                cnt = search({}, group, i, j, group_index, "")
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
4 5
#####
#...#
#...#
#####
'''

'''
5 5
.....
.....
.###.
.###.
.....
'''

Submission status: Runtime Error

Solution code:
H,W = list(map(int,input().split()))
S = []
S_ = ''
for i in range(H):
    tmp = input()
    S.append(tmp)
    S_ = S_ + tmp

def dfs(h,w,mark,hs,ws,check,res):
    check[h][w] = 1
    for i,j in [[-1,0],[0,-1],[0,1],[1,0]]:
        if 0 <= h+i < H and 0 <= w+j < W and check[h+i][w+j] == 0:
            if mark == '#' and S[h+i][w+j] == '.':
                if [hs, ws, h+i, w+j] not in res:
                    res.append([hs, ws, h+i, w+j])
                res, check = dfs(h+i,w+j,'.',hs,ws,check,res)
            elif mark == '.' and S[h+i][w+j] == '#':
                res, check = dfs(h+i,w+j,'#',hs,ws,check,res)
    return res, check

l = []
for i,c in enumerate(S_):
    if c == '#':
        l.append([int(i/W),int(i%W)])

res = []
for h,w in l:
    check = [[0 for j in range(W)] for i in range(H)]
    res,_ = dfs(h, w, '#', h, w, check, res)

print(len(res))

Submission status: Runtime Error

Solution code:
h,w = [int(x) for x in input().split(" ")]

s =[input() for _ in range(h)]

used = [[0] * w for _ in range(h)]
wh = 0; bl=0

def findNext(i, j):
  global h; global w
  global s
  global wh; global bl
  global used
  
  used[i][j] = 1

  for di, dj in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
    i_next = i + di; j_next = j + dj
    if (0 <= i_next  < h) and (0 <= j_next < w):
      if (s[i][j] != s[i_next][j_next]):
        if used[i_next][j_next]==0:
          if s[i][j] == "#":
            wh += 1 
          else:
            bl += 1
          findNext(i_next, j_next)


all_cnt = 0
for i in range(h):
  for j in range(w):
    if used[i][j] == 0:
      wh = 0; bl = 0
      if s[i][j]=="#":
        bl = 1
      else:
        wh = 1
      findNext(i, j)
      all_cnt += wh * bl

print(all_cnt)

Submission status: Runtime Error

Solution code:
h, w = list(map(int, input().split()))
field = [input() for _ in range(h)]


dx = [0, 0, 1, -1]
dy = [-1,1, 0, 0]
is_visited = [[False for _ in range(w)] for _ in range(h)]
is_counted = [[False for _ in range(w)] for _ in range(h)]



def dfs(x, y):
    global ans
    # print("now at {}. color is {}".format((x, y), field[x][y]))
    if field[x][y]=='.' and not is_counted[x][y]:
        # print("end is ", (x, y))
        ans += 1
        is_counted[x][y]=True

    is_visited[x][y] = True
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if( 0 <= nx <= h-1 ) and (0 <= ny <= w-1) and not(is_visited[nx][ny]) and field[x][y]!=field[nx][ny]:
            dfs(nx, ny)
    is_visited[x][y] = False

ans = 0
for x in range(h):
    for y in range(w):
        if field[x][y] == "#":
            is_counted = [[False for _ in range(w)] for _ in range(h)]
            # print("start is ", (x, y))
            dfs(x, y)


print(ans)

Submission status: Runtime Error

Solution code:
h,w = [int(x) for x in input().split(" ")]

s =[input() for _ in range(h)]

used = [[0] * w for _ in range(h)]
wh = 0; bl=0

def findNext(i, j):
  global wh
  global bl
  global used
  
  used[i][j] = 1

  for di, dj in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
    i_next = i + di; j_next = j + dj
    if (0 <= i_next  < h) and (0 <= j_next < w):
      if (s[i][j] != s[i_next][j_next]):
        if used[i_next][j_next]==0:
          if s[i][j] == "#":
            wh += 1 
          else:
            bl += 1
          findNext(i_next, j_next)


all_cnt = 0
for i in range(h):
  for j in range(w):
    if used[i][j] == 0:
      wh = 0; bl = 0
      if s[i][j]=="#":
        bl = 1
      else:
        wh = 1
      findNext(i, j)
      all_cnt += wh * bl

print(all_cnt)

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index, prev):
    num = 0
    if not(current_h > -1 and current_w > -1 and current_h < H and current_w < W):
        return num
    if current_h in search_map:
        if current_w in search_map[current_h]:
            return num
    current = s_map[current_h][current_w]
    if current == prev:
        return num
    if current_h not in search_map:
        search_map[current_h] = {}
    search_map[current_h][current_w] = True

    if (current == '.'):
        num += 1
    else:
        group[current_h][current_w] = group_index
    # 上
    num += search(search_map, group, current_h+1, current_w, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h-1, current_w, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h, current_w+1, group_index, s_map[current_h][current_w])
    num += search(search_map, group, current_h, current_w-1, group_index, s_map[current_h][current_w])
    '''
    if current_h > 0:
        next_h = current_h-1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 下
    if current_h < H-1:
        next_h = current_h+1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 左
    if current_w > 0:
        next_h = current_h
        next_w = current_w-1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 右
    if current_w < W-1:
        next_h = current_h
        next_w = current_w+1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    '''
    return num


def is_map_exist(m, h, w):
    if h in m:
        if w in m[h]:
            return True
    return False


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

s_map = {}
group = {}
searched_map = {}


for i in range(H):
    s_map[i] = {}
    group[i] = {}
    w = list(A[i])
    for j in range(W):
        s_map[i][j] = w[j]
        group[i][j] = 0

ans = 0
group_index = 1
group_cnt = {}
group_cnt[0] = 0
for i in range(H):
    for j in range(W):
        if s_map[i][j] == '#':
            if group[i][j] != 0:
                ans += group_cnt[group[i][j]]
            else:
                cnt = search({}, group, i, j, group_index, "")
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
4 5
#####
#...#
#...#
#####
'''

'''
5 5
.....
.....
.###.
.###.
.....
'''

Submission status: Runtime Error

Solution code:
h,w = [int(x) for x in input().split(" ")]

s =[input() for _ in range(h)]

used = [[0] * w for _ in range(h)]
wh = 0; bl=0

def findNext(i, j):
  global wh
  global bl
  global used
  
  used[i,j] = 1

  for di, dj in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
    i_next = i + di; j_next = j + dj
    if (0 <= i_next  < h) and (0 <= j_next < w):
      if (s[i][j] != s[i_next][j_next]):
        if used[i_next, j_next]==0:
          if s[i][j] == "#":
            wh += 1 
          else:
            bl += 1
          findNext(i_next, j_next)


all_cnt = 0
for i in range(h):
  for j in range(w):
    if used[i, j] == 0:
      wh = 0; bl = 0
      if s[i][j]=="#":
        bl = 1
      else:
        wh = 1
      findNext(i, j)
      all_cnt += wh * bl

print(all_cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
answer = 0
s = []
top = 0
bottom = 0
right = 0
left = 0


def bfs(takasa, haba):
    ans = ''
    top = 0
    bottom = 0
    right = 0
    left = 0
    flag = 0
    queue = deque([[takasa, haba]])
    ans += s[takasa][haba]
    while queue:
        takasa, haba = queue.popleft()
        if takasa == 0:
            top += 1
        if takasa == height-1:
            bottom += 1
        if haba == 0:
            left += 1
        if haba == wide-1:
            right += 1
        if s[takasa][haba] == '#':
            flag+=1
        s[takasa][haba] = '_'
        if top > 0 and left > 0 and right > 0:
            j, k = 1, 0
            new_takasa, new_haba = takasa+j, haba+k
            if flag > 0 and s[new_takasa][new_haba] == '.':
                ans += '.'
                queue.append([new_takasa, new_haba])
            if flag == 0 and s[new_takasa][new_haba] == '#':
                ans += '#'
                queue.append([new_takasa, new_haba])
        elif bottom > 0 and left > 0 and right > 0:
            j, k = -1, 0
            new_takasa, new_haba = takasa+j, haba+k
            if flag > 0 and s[new_takasa][new_haba] == '.':
                ans += '.'
                queue.append([new_takasa, new_haba])
            if flag == 0 and s[new_takasa][new_haba] == '#':
                ans += '#'
                queue.append([new_takasa, new_haba])
        elif top > 0 and left > 0:
            for j, k in([1, 0], [0, 1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif top > 0 and right > 0:
            for j, k in([1, 0], [0, -1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif top > 0:
            for j, k in ([1, 0], [0, 1], [0, -1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif bottom > 0 and left > 0:
            for j, k in([0, 1], [-1, 0]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif bottom > 0 and right > 0:
            for j, k in([0, -1], [-1, 0]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif bottom > 0:
            for j, k in ([-1, 0], [0,1], [0,-1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif left > 0:
            for j, k in ([-1, 0], [1, 0], [0, 1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        elif right > 0:
            for j, k in([-1, 0], [1, 0], [0, -1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag == 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        else:
            for j, k in([1, 0], [0, 1], [-1, 0], [0, -1]):
                new_takasa, new_haba = takasa+j, haba+k
                if flag > 0 and s[new_takasa][new_haba] == '.':
                    ans += '.'
                    queue.append([new_takasa, new_haba])
                if flag < 0 and s[new_takasa][new_haba] == '#':
                    ans += '#'
                    queue.append([new_takasa, new_haba])
        
        top = 0
        bottom = 0
        right = 0
        left = 0
        flag=0
    return(ans.count('#')*ans.count('.'))


height, wide = map(int, input().split())

s = []
for _ in range(height):
    s.append(list(input()))
for takasa in range(height):
    for haba in range(wide):
        if s[takasa][haba] == '#':
            answer += bfs(takasa, haba)
        else:
            pass
print(answer)

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index):
    num = 0
    current = ''
    if current_h not in search_map:
        search_map[current_h] = {}
    search_map[current_h][current_w] = True
    if is_map_exist(s_map, current_h, current_w):
        current = s_map[current_h][current_w]
    else:
        current = ''
    if (current == '.'):
        num += 1
    else:
        if is_map_exist(group, current_h, current_w):
            group[current_h][current_w] = group_index
    # 上
    if current_h > 0:
        next_h = current_h-1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 下
    if current_h < H-1:
        next_h = current_h+1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 左
    if current_w > 0:
        next_h = current_h
        next_w = current_w-1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 右
    if current_w < W-1:
        next_h = current_h
        next_w = current_w+1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    return num


def is_map_exist(m, h, w):
    if h in m:
        if w in m[h]:
            return True
    return False


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

s_map = {}
group = {}
searched_map = {}


for i in range(H):
    s_map[i] = {}
    group[i] = {}
    w = list(A[i])
    for j in range(W):
        s_map[i][j] = w[j]
        group[i][j] = 0

ans = 0
group_index = 1
group_cnt = {}
group_cnt[0] = 0
for i in range(H):
    for j in range(W):
        if s_map[i][j] == '#':
            if group[i][j] != 0:
                ans += group_cnt[group[i][j]]
            else:
                cnt = search({}, group, i, j, group_index)
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
4 5
#####
#...#
#...#
#####
'''

'''
5 5
.....
.....
.###.
.###.
.....
'''

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index):
    num = 0
    if current_h not in search_map:
        search_map[current_h] = {}
    search_map[current_h][current_w] = True
    if is_map_exist(s_map, current_h, current_w):
        current = s_map[current_h][current_w]
    else:
        current = ''
    if (current == '.'):
        num += 1
    else:
        if is_map_exist(group, current_h, current_w):
            group[current_h][current_w] = group_index
    # 上
    if current_h > 0:
        next_h = current_h-1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 下
    if current_h < H-1:
        next_h = current_h+1
        next_w = current_w
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 左
    if current_w > 0:
        next_h = current_h
        next_w = current_w-1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    # 右
    if current_w < W-1:
        next_h = current_h
        next_w = current_w+1
        isExist = False
        if next_h in search_map:
            if next_w in search_map[next_h]:
                isExist = True
        if is_map_exist(s_map, next_h, next_w):
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h,
                              next_w, group_index)
    return num


def is_map_exist(m, h, w):
    if h in m:
        if w in m[h]:
            return True
    return False


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

s_map = {}
group = {}
searched_map = {}


for i in range(H):
    s_map[i] = {}
    group[i] = {}
    w = list(A[i])
    for j in range(W):
        s_map[i][j] = w[j]
        group[i][j] = 0

ans = 0
group_index = 1
group_cnt = {}
group_cnt[0] = 0
for i in range(H):
    for j in range(W):
        if s_map[i][j] == '#':
            if group[i][j] != 0:
                ans += group_cnt[group[i][j]]
            else:
                cnt = search({}, group, i, j, group_index)
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
4 5
#####
#...#
#...#
#####
'''

'''
5 5
.....
.....
.###.
.###.
.....
'''

Submission status: Runtime Error

Solution code:
import numpy as np

h,w = [int(x) for x in input().split(" ")]

s = []
for i in range(h):
  s.append(input())


used = np.zeros((h,w))
wh = 0; bl=0

def findNext(i, j):
  global wh
  global bl
  global used
  
  used[i,j] = 1
  i_next = i - 1; j_next = j
  if (i_next >= 0) and (i_next < h) and (j_next >= 0) and (j_next < w):
    if (s[i][j] != s[i_next][j_next]):
      if used[i_next, j_next]==0:
        if s[i][j] == "#":
          wh += 1 
        else:
          bl += 1
        findNext(i_next, j_next)
  i_next = i; j_next = j+1
  if (i_next >= 0) and (i_next < h) and (j_next >= 0) and (j_next < w):
    if (s[i][j] != s[i_next][j_next]):
      if used[i_next, j_next]==0:
        if s[i][j] == "#":
          wh += 1 
        else:
          bl += 1
        findNext(i_next, j_next)
  i_next = i; j_next = j-1
  if (i_next >= 0) and (i_next < h) and (j_next >= 0) and (j_next < w):
    if (s[i][j] != s[i_next][j_next]):
      if used[i_next, j_next]==0:
        if s[i][j] == "#":
          wh += 1 
        else:
          bl += 1
        findNext(i_next, j_next)
  i_next = i + 1; j_next = j
  if (i_next >= 0) and (i_next < h) and (j_next >= 0) and (j_next < w):
    if (s[i][j] != s[i_next][j_next]):
      if used[i_next, j_next]==0:
        if s[i][j] == "#":
          wh += 1 
        else:
          bl += 1
        findNext(i_next, j_next)

all_cnt = 0
for i in range(h):
  for j in range(w):
    if used[i, j] == 0:
      wh = 0; bl = 0
      if s[i][j]=="#":
        bl = 1
      else:
        wh = 1
      findNext(i, j)
      all_cnt += wh * bl

print(all_cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w = map(int, input().split())
colors = np.zeros((h,w))
for i in range(h):
    s = input()
    for j,c in enumerate(s):
        if c=='#':
            colors[i,j] = 1

def tonari(pos):
    i,j = pos
    ans = []
    if i==0:
        ans.append((i+1,j))
    elif i==h-1:
        ans.append((i-1,j))
    else:
        ans.append((i-1,j))
        ans.append((i+1,j))
    if j==0:
        ans.append((i,j+1))
    elif j==w-1:
        ans.append((i,j-1))
    else:
        ans.append((i,j-1))
        ans.append((i,j+1))
    
    return ans

# 深さ優先探索をする
# マス(0,0)からスタート
# 次の仕切り直しで使うスタート位置のリストnext_start
# すでに訪ねたかどうかを表すh*wの表 visited=1 
# current_positionの前後左右を見てもしcurrent_colorと異なればそっちに動く

current_position = (0,0)
current_color = colors[current_position]
current_group = 0
groups = {0:[1-current_color, current_color]} # {group_num:(white_num,black_num)}
next_visit = []
other_group = []

unvisited = [(i,j) for i in range(h) for j in range(w)]
unvisited.remove((0,0))

while True:
    # current_positionの前後左右を見る
    #print(current_position, groups)
    neighbors = tonari(current_position)
    for ngb in neighbors:
        if colors[ngb] != current_color and ngb in unvisited and ngb not in next_visit:
            next_visit.append(ngb)
        # もしnon_visitedかつcurrent_colorと異なればnext_visitに追加
        # それ以外は何もしない
    
    #print(next_visit)

    # next_visitが空でないとき
    if len(next_visit) > 0:
        current_position = next_visit.pop()
        current_color = colors[current_position]
        groups[current_group][int(current_color)] += 1
        unvisited.remove(current_position)
        # next_visitの中から次の移動先を取り出して
        # colorをみてgroupに追加
        # current_positionとcurrent_colorを更新

    elif len(next_visit) == 0:
        # next_visitが空のとき
        if len(unvisited) == 0:
            break
        else:
            current_position = unvisited.pop()
            current_color = colors[current_position]
            current_group += 1
            groups[current_group] = [1-current_color, current_color]
            # unvisitedの中から一つ選んで
            # current_position, color更新
            # current_group更新

#print(groups)
ans = 0
for g in groups:
    black, white = groups[g]
    ans += black*white
print(int(ans))

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index):
    if not(current_h > -1 and current_w > -1 and current_h < H and current_w < W):
        return 0
    num = 0
    if current_h not in search_map:
        search_map[current_h] = {}
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    if (current == '.'):
        num += 1
    else:
        group[current_h][current_w] = group_index
    # 上
    if current_h > 0:
        next_h = current_h-1
        next_w = current_w
        if next_h > -1 and next_w > -1 and next_h < H and next_w < W:
            isExist = False
            if next_h in search_map:
                if next_w in search_map[next_h]:
                    isExist = True
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h, next_w, group_index)
    # 下
    if current_h < H-1:
        next_h = current_h+1
        next_w = current_w
        if next_h > -1 and next_w > -1 and next_h < H and next_w < W:
            isExist = False
            if next_h in search_map:
                if next_w in search_map[next_h]:
                    isExist = True
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h, next_w, group_index)
    # 左
    if current_w > 0:
        next_h = current_h
        next_w = current_w-1
        if next_h > -1 and next_w > -1 and next_h < H and next_w < W:
            isExist = False
            if next_h in search_map:
                if next_w in search_map[next_h]:
                    isExist = True
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h, next_w, group_index)
    # 右
    if current_w < W-1:
        next_h = current_h
        next_w = current_w+1
        if next_h > -1 and next_w > -1 and next_h < H and next_w < W:
            isExist = False
            if next_h in search_map:
                if next_w in search_map[next_h]:
                    isExist = True
            if s_map[next_h][next_w] != current and not(isExist):
                num += search(search_map, group, next_h, next_w, group_index)
    return num


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

s_map = {}
group = {}
searched_map = {}


for i in range(H):
    s_map[i] = {}
    group[i] = {}
    w = list(A[i])
    for j in range(W):
        s_map[i][j] = w[j]
        group[i][j] = 0

ans = 0
group_index = 1
group_cnt = {}
group_cnt[0] = 0
for i in range(H):
    for j in range(W):
        if s_map[i][j] == '#':
            if group[i][j] != 0:
                ans += group_cnt[group[i][j]]
            else:
                cnt = search({}, group, i, j, group_index)
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
4 5
#####
#...#
#...#
#####
'''

'''
5 5
.....
.....
.###.
.###.
.....
'''