#K47557. Minesweeper: Reveal Safe Cells

    ID: 28224 Type: Default 1000ms 256MiB

Minesweeper: Reveal Safe Cells

Minesweeper: Reveal Safe Cells

You are given a Minesweeper board represented as a grid of characters. Each cell in the board is either unrevealed (E), a revealed blank cell (B), a digit (from 1 to 8) indicating the number of adjacent mines, or a mine (M). Given a starting cell, your task is to update the board by revealing all safe cells using a depth-first search (DFS) approach.

If the starting cell is a mine (M), the board remains unchanged. If it is an unrevealed cell (E), reveal it. If the revealed cell has one or more adjacent mines, it should display the count of associated mines. Otherwise, if the count is 0, mark it as a blank (B) and recursively reveal its adjacent cells.

The adjacent cells of a cell located at $(x, y)$ are the eight neighbors: $(x-1,y-1)$, $(x-1,y)$, $(x-1,y+1)$, $(x,y-1)$, $(x,y+1)$, $(x+1,y-1)$, $(x+1,y)$, $(x+1,y+1)$, provided these positions are within the bounds of the board.

Note: Use stdin for input and stdout for output. All formulas, if any, are expressed in \( \LaTeX \) format.

inputFormat

The first line contains two integers, \(R\) and \(C\), representing the number of rows and columns of the board.

The next \(R\) lines each contain \(C\) space-separated characters representing the cells of the board.

The last line contains two integers, representing the row and column indices (0-indexed) of the starting cell.

outputFormat

Output the updated board in \(R\) lines. Each line should contain \(C\) space-separated characters representing the state of each cell after applying the update procedure.

## sample
4 5
E E E E E
E E M E E
E E E E E
E E E E E
3 0
B 1 E 1 B

B 1 M 1 B B 1 1 1 B B B B B B

</p>