// Arup Guha
// 11/5/2016
// Solution to SER 2016 Division II Problem G: Islands (written in contest, also in D1)

import java.util.*;

public class islands {

	final public static int[] DX = {-1,0,0,1};
	final public static int[] DY = {0,-1,1,0};
	public static int r;
	public static int c;
	public static char[][] grid;

	public static void main(String[] args) {

		// Read the grid.
		Scanner stdin = new Scanner(System.in);
		r = stdin.nextInt();
		c = stdin.nextInt();
		grid = new char[r][];
		for (int i=0; i<r; i++)
			grid[i] = stdin.next().toCharArray();

		// Solve it.
		System.out.println(solve());
	}

	public static int solve() {

		// Easy case.
		if (!contains('L')) return 0;

		// Set up search.
		int res = 0;
		boolean[][] used = new boolean[r][c];
		
		// Go from each unfilled square and count.
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {
				if (!used[i][j] && grid[i][j] == 'L') {
					res++;
					fill(i,j,used);
				}
			}
		}
		return res;
	}

	// Note: A regular flood fill works but in contest I didn't get correct at first, so I changed my flood fill 
	// to a BFS. Turns out that they just screwed up the mirror site, so the dfs would have been fine...
	public static void fill(int x, int y, boolean[][] used) {

		// Mark it and set up BFS/
		used[x][y] = true;
		LinkedList<Integer> q = new LinkedList<Integer>();
		q.offer(x*c+y);

		// Run it.
		while (q.size() > 0) {

			// Get next item,
			int cur = q.poll();
			int curX = cur/c;
			int curY = cur%c;

			// Go to neighbors...
			for (int i=0; i<DX.length; i++) {
				int nextX = curX + DX[i];
				int nextY = curY + DY[i];
				
				// Can't go out of bounds or to used squars.
				if (!inbounds(nextX, nextY)) continue;
				if (used[nextX][nextY]) continue;
				
				// Can't go to W, otherwise, go for it.
				if (grid[nextX][nextY] != 'W') {
					used[nextX][nextY] = true;
					q.offer(nextX*c+nextY);
				}
			}
		}


	}

	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < r && y >= 0 && y < c;
	}

	public static boolean contains(char ch) {

		for (int i=0; i<r; i++)
			for (int j=0; j<c; j++)
				if (grid[i][j] == ch)
					return true;
		return false;

	}
}