from collections import deque n,m = map(int,input().split()) graph = [] for i in range(n): graph.append(list(input())) count = 0 visited = [[False] * m for _ in range(n)] directions = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)] for i in range(n): for j in range(m): if graph[i][j] == "W" and not visited[i][j]: count += 1 queue = deque() visited[i][j] = True queue.append((i,j)) while queue: di, dj = queue.popleft() for ni,nj in directions: newi,newj = di+ni,dj+nj if 0 <= newi < n and 0 <= newj < m: if graph[newi][newj] == "W" and not visited[newi][newj]: visited[newi][newj] = True queue.append((newi,newj)) print(count)