Minimum number of islands

Given a grid of land, water, and cloud cells, find the minimum possible number of 4-connected land islands if every cloud can be either land or water.

Medium5GraphDFSGreedyUnion-findInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

You are mapping a faraway planet with a satellite. The planet's surface is a grid, and the satellite captured one image of that surface. Each grid cell is land (written as 'L'), water (written as 'W'), or covered by clouds (written as 'C'). A cell covered by clouds can be either land or water, and you cannot tell which.

An island is a region of land cells in which every two cells are connected to each other by a path that only moves up, down, left, or right.

Given the image, find the smallest number of islands that is consistent with it.

Input

The first line contains the number of rows rr and the number of columns cc of the image (1r,c501 \le r, c \le 50).

Each of the next rr lines contains exactly cc characters. Every character is 'L' for land, 'W' for water, or 'C' for clouds.

Output

Print a single integer, the smallest possible number of islands.