from collections import deque
grid = []
m, n = map(int, input().split())
for i in range(m):
s = input()
grid.append( [val for val in s] )
def bfs(i, j):
que = deque()
que.append((i, j))
visited = {(i, j)}
while len(que) > 0:
ii, jj = que.popleft()
grid[ii][jj] = '.'
for new_i, new_j in [ [ii-1, jj-1], [ii-1, jj], [ii-1, jj+1], [ii, jj-1], [ii, jj+1], [ii+1, jj-1], [ii+1, jj], [ii+1, jj+1] ]:
if new_i >= 0 and new_i < m and new_j >= 0 and new_j < n and grid[new_i][new_j] == 'W' and (new_i, new_j) not in visited:
visited.add((new_i, new_j))
que.append((new_i,new_j))
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 'W':
ans += 1
bfs(i, j)
print(ans)