from collections import deque

h, w = map(int, input().split()) grid = [] for _ in range(h): line = input().strip() grid.append(list(line))

visited = [[False for _ in range(w)] for _ in range(h)] count = 0 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for i in range(h): for j in range(w): if grid[i][j] == 'Y' and not visited[i][j]: queue = deque() queue.append((i, j)) visited[i][j] = True while queue: x, y = queue.popleft() for dx, dy in directions: nx = x + dx ny = y + dy if 0 <= nx < h and 0 <= ny < w: if grid[nx][ny] == 'Y' and not visited[nx][ny]: visited[nx][ny] = True queue.append((nx, ny)) count += 1

print(count)

2 条评论

  • 1