def dfs(grid, row, col):
directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
grid[row][col] = '.'
for d in directions:
new_row = row + d[0]
new_col = col + d[1]
if 0 <= new_row < len(grid) and 0 <= new_col < len(grid[0]) and grid[new_row][new_col] == 'W':
dfs(grid, new_row, new_col)
def count_water_pools(grid):
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 'W':
dfs(grid, i, j)
count += 1
return count
N, M = map(int, input().split())
grid = [list(input()) for _ in range(N)]
print(count_water_pools(grid))