#P7645. Island Map After Sea Level Rise

    ID: 20837 Type: Default 1000ms 256MiB

Island Map After Sea Level Rise

Island Map After Sea Level Rise

You are given a map of an archipelago represented as an \( R \times C \) grid. Each cell is either land denoted by X or sea denoted by .. It is estimated that in 50 years the sea level will rise and submerge every piece of land which is currently surrounded by sea on at least three of its four neighboring sides. Note that neighbors are considered only in the four cardinal directions (up, down, left, right), and any cell outside the boundaries of the map is assumed to be sea.

Your task is to compute the state of the archipelago after 50 years. Since the surviving land might be much smaller than the original map, you only need to output the smallest rectangular region that contains all remaining land cells.

The submergence rule can be expressed mathematically as: a land cell at position \( (i,j) \) will become sea if \[ \text{# of adjacent sea cells} \ge 3. \]

inputFormat

The first line contains two integers \(R\) and \(C\) (the number of rows and columns respectively). Each of the following \(R\) lines contains a string of length \(C\), representing the map. Each character is either X or ..

outputFormat

Output the smallest rectangular section of the map that encloses all land cells remaining after 50 years. Each line should represent a row of the resulting map. If no land remains, output nothing.

sample

3 3
XXX
X.X
XXX
XXX

X.X XXX

</p>