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:
def search(search_map, group, current_h, current_w, group_index, calc):
    calc[0] += 1
    if current_h in search_map:
        search_map[current_h]
    else:
        search_map[current_h] = {}
        search_map[current_h][current_w] = True
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    num = 0
    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
        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, calc)
    # 下
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 左
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 右
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    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
calc = [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]]
                calc[0] += 1
            else:
                cnt = search({}, group, i, j, group_index, calc)
                #group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
1 1
#
'''

'''
1 1
.
'''

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

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


'''
3 1
#
.
#
'''


'''
3 3
###
#.#
###
'''

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

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

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

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

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index, calc):
    calc[0] += 1
    if current_h in search_map:
        search_map[current_h]
    else:
        search_map[current_h] = {}
        search_map[current_h][current_w] = True
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    num = 0
    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
        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, calc)
    # 下
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 左
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 右
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    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
calc = [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]]
                calc[0] += 1
            else:
                #cnt = search({}, group, i, j, group_index, calc)
                #group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
1 1
#
'''

'''
1 1
.
'''

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

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


'''
3 1
#
.
#
'''


'''
3 3
###
#.#
###
'''

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

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

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

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

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index, calc):
    calc[0] += 1
    if current_h in search_map:
        search_map[current_h]
    else:
        search_map[current_h] = {}
        search_map[current_h][current_w] = True
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    num = 0
    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
        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, calc)
    # 下
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 左
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 右
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    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
calc = [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]]
                # calc[0] += 1
            else:
                cnt = search({}, group, i, j, group_index, calc)
                group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
1 1
#
'''

'''
1 1
.
'''

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

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


'''
3 1
#
.
#
'''


'''
3 3
###
#.#
###
'''

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

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

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

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

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 = ''
    flag = 0
    queue = deque([[takasa, haba]])
    ans += s[takasa][haba]
    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
        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])
                s[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])
                s[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])
                    s[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])
                    s[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])
                    s[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])
                    s[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])
                    s[new_takasa][new_haba] = '_'
        elif bottom > 0:
            for j, k in ([-1, 0], [1, 0], [-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])
                    s[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])
                    s[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])
                    s[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])
                    s[new_takasa][new_haba] = '_'
    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)
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(160000)

H,W = map(int,input().split(' '))
S = []
for i in [0]*H:
	l = input()
	S.append([1 if x == '#' else -1 for x in l])

bk = 0
wh = 0
def check(S,x,y):
	global bk
	global wh
	if S[y][x] == 0:
		return
	if S[y][x] == 1:
		bk += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == -1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == -1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == -1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == -1:
			check(S,x+1,y)
		return
	if S[y][x] == -1:
		wh += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == 1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == 1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == 1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == 1:
			check(S,x+1,y)
		return

cnt = 0
for y in range(H):
	for x in range(W):
		if S[y][x] == 1:
			check(S,x,y)
			cnt += wh*bk
			wh = 0
			bk = 0
print(cnt)

Submission status: Runtime Error

Solution code:
import copy
H, W = (list)(map(int, input().split()))
grid = [[lin for lin in input()] for _ in range(H)]

mat = [['&'] * (W+2) for _ in range(H+2)]
for row in range(1, H+1):
    for col in range(1, W+1):
        mat[row][col] = grid[row-1][col-1]


def search(x, row, col, mat):
    ran = [-1, 1]
    ret = []
    for i in ran:
        if mat[row+i][col] == x:
            ret.append([row+i, col])
        if mat[row][col+i] == x:
            ret.append([row, col+i])

    return ret


def bfs(row, col, mat):
    ret = 0
    mat[row][col] = '&'

    res = search('.', row, col, mat)

    ret += len(res)
    for cand in res:
        mat[cand[0]][cand[1]] = '&'

    for cand in res:
        res2 = search('#', cand[0], cand[1], mat)
        for cand2 in res2:
            res3 = bfs(cand2[0], cand2[1], mat)
            ret += res3
    return ret


ans = 0

for row in range(1, H+1):
    for col in range(1, W+1):
        if mat[row][col] == '#':
            mat_copy = copy.deepcopy(mat)
            ans += bfs(row, col, mat_copy)

print(ans)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):#h:高さ
    s.append([str(m) for m in list(input())])
dd = ((0, -1), (-1, 0), (0, 1), (1, 0))
ans = 0
t = []

for i in range(h):
    t.append([])
for i in range(h):
    for j in range(w):
        t[i].append(0)
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            tmp = 0
            for dx, dy in dd:
                nx = j + dx
                ny = i + dy
                if 0 <= nx < w and 0 <= ny < h:
                    if s[ny][nx] == '.':
                        #t[ny][nx] +=
                        t[ny][nx] += 1
                        tmp += 1


            for u in range(1, 5):
                if tmp == u:
                    t[i][j] += tmp - 1
            for dx, dy in dd:
                nx = j + dx
                ny = i + dy
                if 0 <= nx < w and 0 <= ny < h:
                    if s[ny][nx] == '.':
                        # t[ny][nx] +=
                        t[ny][nx] += t[i][j]

an = 0

for i in range(h):
    for j in range(w):
        if s[ny][nx] == '.':
            an += t[i][j]
print(an)

Submission status: Runtime Error

Solution code:
from collections import deque

H,W = map(int,input().split())
S = []
for i in range(H):
    S.append(list(str(input())))
count = 0
stuck = 0
j = 0
for j in range(H):
    k = 0
    for k in range(W):
        table = [[-1 for w in range(W)] for h in range(H)]
        if S[j][k] == "#":
            hNow = j
            wNow = k
            while stuck < 4:
                stuck = 0
                if hNow > 0:
                    if S[hNow][wNow] != S[hNow-1][wNow]:
                        if table[hNow-1][wNow] < 0:
                            if S[hNow-1][wNow] == ".":
                                count+=1
                            else:
                                pass
                            table[hNow-1][wNow] = 0
                        else:
                            stuck+=1
                        hNow-=1
                    else:
                        stuck+=1
                else:
                    stuck+=1
                if wNow > 0:
                    if S[hNow][wNow] != S[hNow][wNow-1]:
                        if table[hNow][wNow-1] < 0:
                            if S[hNow][wNow-1] == ".":
                                count+=1
                            else:
                                pass
                            table[hNow][wNow-1] = 0
                        else:
                            stuck+=1
                        wNow-=1
                    else:
                        stuck+=1
                else:
                    stuck+=1
                if hNow < H:
                    if S[hNow][wNow] != S[hNow+1][wNow]:
                        if table[hNow+1][wNow] < 0:
                            if S[hNow+1][wNow] == ".":
                                count+=1
                            else:
                                pass
                            table[hNow+1][wNow] = 0
                        else:
                            stuck+=1
                        hNow+=1
                    else:
                        stuck+=1
                    
                else:
                    stuck+=1
                if wNow < W:
                    if S[hNow][wNow] != S[hNow][wNow+1]:
                        if table[hNow][wNow+1] < 0:
                            if S[hNow][wNow+1] == ".":
                                count+=1
                            else:
                                pass
                            table[hNow][wNow+1] = 0
                        else:
                            stuck+=1
                        wNow+=1
                    else:
                        stuck+=1
                
                else:
                    stuck+=1
                
        else:
            pass
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

N, Q = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()
X = []
for i in range(Q):
    X.append(int(input().strip()))

for i in range(Q):
    ans = 0
    a = np.array(A)
    a = a - X[i]
    a_ = np.absolute(a)
    a_ = a_.tolist()
    b = A
    
    for j in range(N):
        c = copy.copy(a_)
        d = copy.copy(b)
        if j%2 == 0:
            ans += max(d)
            d.remove(max(d))
            c.remove(c[len(c)-1])
        else:
            d.remove(d[c.index(min(c))])
            c.remove(min(c))

    print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split(' '))
S = []
for i in [0]*H:
	l = input()
	S.append([1 if x == '#' else -1 for x in l])

bk = 0
wh = 0
def check(S,x,y):
	global bk
	global wh
	if S[y][x] == 0:
		return
	if S[y][x] == 1:
		bk += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == -1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == -1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == -1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == -1:
			check(S,x+1,y)
		return
	if S[y][x] == -1:
		wh += 1
		S[y][x]=0
		if 0 < y and S[y-1][x] == 1:
			check(S,x,y-1)
		if 0 < x and S[y][x-1] == 1:
			check(S,x-1,y)
		if y < H-1 and S[y+1][x] == 1:
			check(S,x,y+1)
		if x < W-1 and S[y][x+1] == 1:
			check(S,x+1,y)
		return

cnt = 0
for y in range(H):
	for x in range(W):
		if S[y][x] == 1:
			check(S,x,y)
			cnt += wh*bk
			wh = 0
			bk = 0
print(cnt)

Submission status: Runtime Error

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

def visalizer():
    global field
    print("=========================")
    for i in range(h):
        for j in range(w):
            print(field[i][j], end=' ')
        print('\n')
    print("=========================")

def maeke_last(c):
    if c == "#":
        return "."
    else:
        return "#"

pair_list = []
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
def dfs( last, s_row=0, s_colmun=0, row=0, column=0,):
    global field
    global dict
    if not((0 <= row and row <=h-1) and (0 <=column and column <= w-1)):
        return
    if field[row][column] == '@':
        return

    here = field[row][column]

    if here == last:
        return
    else:
        if not ((s_row, s_colmun) ,(row, column)) in pair_list:
            if here == '.':
                pair_list.append(((s_row, s_colmun) ,(row, column)))


    here_was = field[row][column]
    field[row][column] = '@'
    # visalizer()
    for i in range(len(dx)):
        n_row = row + dx[i]
        n_column = column + dy[i]
        dfs(last = here, s_row = s_row, s_colmun=s_colmun, row=n_row, column=n_column)
    field[row][column] = here_was

# for s_row in range(h):
#     for s_column in range(w):
#         dfs(last = field[s_row][s_column],  s_row=s_row, s_colmun=s_column, row=s_row, column=s_column)

for s_row in range(h):
    for s_column in range(w):
        if field[s_row][s_column] == "#":
            dfs(last = maeke_last(field[s_row][s_column]),  s_row=s_row, s_colmun=s_column, row=s_row, column=s_column)

for ele in pair_list:
    if ele[0]==ele[1] :
        pair_list.remove(ele)
    # pair_list.remove((ele[1], ele[0]))

pair_list2 = []
# for ele in pair_list:
#     if (not (ele[1], ele[0]) in pair_list2) :
#         pair_list2.append(ele)

# for ele in pair_list2:
#     print(ele)
print(len(pair_list))


# visalizer()

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index, calc):
    calc[0] += 1
    if current_h in search_map:
        search_map[current_h]
    else:
        search_map[current_h] = {}
        search_map[current_h][current_w] = True
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    num = 0
    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
        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, calc)
    # 下
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 左
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 右
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    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 = {}
calc = [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]]
                calc[0] += 1
            else:
                #cnt = search({}, group, i, j, group_index, calc)
                #group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
1 1
#
'''

'''
1 1
.
'''

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

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


'''
3 1
#
.
#
'''


'''
3 3
###
#.#
###
'''

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

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

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

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

Submission status: Runtime Error

Solution code:
def search(search_map, group, current_h, current_w, group_index, calc):
    calc[0] += 1
    if current_h in search_map:
        search_map[current_h]
    else:
        search_map[current_h] = {}
        search_map[current_h][current_w] = True
    search_map[current_h][current_w] = True
    current = s_map[current_h][current_w]
    num = 0
    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
        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, calc)
    # 下
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 左
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    # 右
    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 s_map[next_h][next_w] != current and not(isExist):
            num += search(search_map, group, next_h, next_w, group_index, calc)
    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 = {}
calc = [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]]
                calc[0] += 1
            else:
                cnt = search({}, group, i, j, group_index, calc)
                #group_cnt[group_index] = cnt
                group_index += 1
                ans += cnt
print(ans)

'''
1 1
#
'''

'''
1 1
.
'''

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

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


'''
3 1
#
.
#
'''


'''
3 3
###
#.#
###
'''

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

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

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

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