Maximum Islands

Given an n by m grid of land, water, and cloud cells where clouds may be either, maximize the number of 4-connected land components.

Medium7GraphDFSDynamic programmingCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

You are mapping a distant planet with a satellite.

The satellite has taken one picture of the planet's surface. The photographed region is a grid. Each cell of the grid is land, water, or covered by clouds. A cell covered by clouds may be land or it may be water, and the picture does not tell you which.

An island is a set of connected land cells. Two cells are connected when they share an edge.

Given the picture, find the largest number of islands that is consistent with it.

Input

The first line contains two integers nn and mm separated by a space. (1n,m401 \le n, m \le 40)

Each of the next nn lines contains mm characters describing the satellite picture. A land cell is written as 'L', a water cell as 'W', and a cell covered by clouds as 'C'.

Output

Print one integer on a single line, the largest number of islands consistent with the given grid.