#K43022. Capture Surrounding Regions

    ID: 27217 Type: Default 1000ms 256MiB

Capture Surrounding Regions

Capture Surrounding Regions

You are given a 2D grid of characters with dimensions \(N \times M\). Each cell contains either an 'X' or an 'O'. Your task is to capture all regions completely surrounded by 'X' by flipping all interior 'O's to 'X'. An 'O' is not flipped if it is on the boundary or is connected to an 'O' on the boundary.

In more formal terms, an 'O' should not be flipped if and only if it is connected to an 'O' on the border (directly or indirectly through other 'O's). You are required to use depth-first search (DFS) or a similar algorithm to mark the safe regions and then flip the remaining 'O's.

Note: All formulas in this problem description are in LaTeX format. For example, the grid dimensions are given as \(N \times M\).

inputFormat

The first line contains two integers \(N\) and \(M\) separated by a space, where \(N\) is the number of rows and \(M\) is the number of columns.

Each of the following \(N\) lines contains a string of exactly \(M\) characters, each being either 'X' or 'O', representing a row of the grid.

outputFormat

Output the modified grid after capturing all surrounded regions, with each row on a new line. Do not add extra spaces.

## sample
4 4
XXXX
XOOX
XXOX
XOXX
XXXX

XXXX XXXX XOXX

</p>