#P6940. Visual Python++ Block Nesting Depth

    ID: 20147 Type: Default 1000ms 256MiB

Visual Python++ Block Nesting Depth

Visual Python++ Block Nesting Depth

In the newly proposed Visual Python++ programming language, a statement block is represented as a rectangle in a grid. The block is defined by its top‐left corner \( (r_{1}, c_{1}) \) and its bottom–right corner \( (r_{2}, c_{2}) \). A programmer simply places the character p at the top–left and the character y at the bottom–right. The parser then automatically recovers the nesting structure of the program.

In a syntactically correct program, every two statement blocks are either nested (one completely contained in the other) or completely disjoint, and their borders do not overlap. A block \(B\) is considered nested in block \(A\) if and only if

[ r_{A} < r_{B} \quad,\quad c_{A} < c_{B} \quad,\quad r'{A} > r'{B} \quad,\quad c'{A} > c'{B} ]

Your task is to compute the maximum nesting depth of the statement blocks in a given grid. The nesting depth of an outer block is 1, and if a block is nested directly inside another block of depth d, then its depth is d+1.

inputFormat

The first line contains two positive integers R and C indicating the number of rows and columns of the grid. Each of the following R lines contains a string of length C consisting of arbitrary characters. It is guaranteed that the grid represents a valid Visual Python++ program where each statement block is marked by a character p at its top–left corner and a character y at its bottom–right corner.

outputFormat

Output a single integer denoting the maximum nesting depth of statement blocks in the input grid.

sample

3 3
p..
...
..y
1