#K45972. Plant Growth Simulation

    ID: 27872 Type: Default 1000ms 256MiB

Plant Growth Simulation

Plant Growth Simulation

You are given a 2D grid representing a garden. Each cell in the grid is either P (a cell where a plant is already growing) or . (an empty cell, i.e. bare ground). During each time step, the plant spreads to its four adjacent cells (up, down, left, and right) if they are empty.

Your task is to simulate the spread of the plant and determine the number of complete time steps required until no further growth is possible.

Note: If the plant is already fully spread (i.e. there is no empty cell adjacent to any plant) or there is no plant at all, the required steps is 0.

The growth process is governed by the following rule: At each time step, all cells that are adjacent to at least one P cell will grow a plant (if they are empty). The operation is performed simultaneously for all cells in each step.

This problem requires you to implement the simulation and output the total number of time steps required for the spreading to complete.

inputFormat

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

N
row1
row2
...
rowN

Where:

  • N is a positive integer indicating the number of rows in the grid.
  • Each row is a string of equal length, composed only of the characters 'P' (a cell with a plant) and '.' (an empty cell).

All rows are guaranteed to have the same number of columns.

outputFormat

Output to standard output (stdout) a single integer representing the number of complete time steps required until no additional plant growth is possible.

## sample
3
.P.
...
P..
2