#C382. Garden Sprinkler Installation

    ID: 47289 Type: Default 1000ms 256MiB

Garden Sprinkler Installation

Garden Sprinkler Installation

You are given one or more garden configurations. Each configuration is represented as a grid of characters. Each cell in the grid is either P (representing a plant) or E (representing an empty space). Two cells are considered connected if they are adjacent vertically or horizontally (i.e. using 4-adjacency, where the neighbors of a cell at \( (x,y) \) are \( (x+1,y) \), \( (x-1,y) \), \( (x,y+1) \), and \( (x,y-1) \)). A single sprinkler can water an entire connected group of plants. Your task is to compute the minimum number of sprinklers needed in each garden configuration, which is equivalent to counting the number of connected components of plant cells in the grid.

Input Format: The input consists of multiple garden configurations. For each configuration, the first line contains two integers \( R \) and \( C \) representing the number of rows and columns in the garden. The next \( R \) lines each contain a string of length \( C \) representing the garden grid. The input terminates with a line containing two zeros: "0 0".

Output Format: For each garden configuration, output a single integer indicating the minimum number of sprinklers needed (i.e. the number of connected components of plants), each on a separate line.

inputFormat

The input is read from standard input (stdin) and has the following structure:

R C
row1
row2
...
rowR
R C
row1
row2
...
rowR
0 0

Here, each row is a string of length C that consists solely of the characters P and E. The sequence of garden configurations ends when a line with "0 0" is encountered.

outputFormat

For each garden configuration in the input, output a single line containing one integer — the minimum number of sprinklers needed to water all the plants.

## sample
3 3
PEP
EEE
PPP
0 0
3

</p>