-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11123-양한마리...양두마리...py
35 lines (29 loc) · 965 Bytes
/
11123-양한마리...양두마리...py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
from collections import deque
input = sys.stdin.readline
T = int(input().rstrip())
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for _ in range(T):
H, W = map(int, sys.stdin.readline().strip().split(' '))
graph = []
for _ in range(H):
graph.append(list(map(str, input().rstrip())))
count = 0
for i in range(H):
for j in range(W) :
if graph[i][j] == '#':
count += 1
queue = deque([[j, i]])
graph[i][j] = '.'
while queue:
x, y = queue.pop()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx < 0 or W <= nx or ny < 0 or H <= ny:
continue
if graph[ny][nx] == '#':
queue.append([nx, ny])
graph[ny][nx] = '.'
print(count)