#C5235. Surrounded Regions

    ID: 48862 Type: Default 1000ms 256MiB

Surrounded Regions

Surrounded Regions

Given a two-dimensional board consisting of characters 'X' and 'O', your task is to capture all regions that are completely surrounded by 'X's. A region is captured by flipping all 'O's into 'X's in that surrounded region. An 'O' should not be flipped if it is on the border, or connected to an 'O' that is on the border. In other words, any 'O' that can reach a border (directly or indirectly via adjacent 'O's in the up, down, left, or right directions) should remain unchanged.

Formally, let the board be represented as a matrix of dimensions (m \times n). For every cell (a_{ij}) with value 'O', if there is no path from (a_{ij}) to any boundary cell (i.e., a cell in the first row, last row, first column, or last column) using only adjacent 'O's, then change (a_{ij}) to 'X'.

inputFormat

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

(m) (n)
Next (m) lines: Each line is a string of length (n) representing a row of the board.

If (m = 0), the board is empty.

outputFormat

Output the modified board to standard output. Each row of the board should be printed on a new line. For an empty board, nothing should be printed to output.## sample

4 4
XXXX
XOOX
XXOX
XOXX
XXXX

XXXX XXXX XOXX

</p>