Suki's Paint Grid Coverage
Suki, a black curly-coated dog with a grey beard and white paws, has discovered a grid of unpainted tiles in the park. She starts painting from a specific tile and, being a curious pup, the paint spreads to all orthogonally adjacent unpainted tiles. She continues painting until all reachable tiles are covered.
Given an n × m grid where each cell is either painted (1) or unpainted (0), a starting position, determine the final state of the grid after the paint spreads, and count the total number of painted cells.
Input
The first line contains n and m (1 ≤ n, m ≤ 500), the dimensions of the grid.
The second line contains r and c (0 ≤ r < n, 0 ≤ c < m), the starting row and column (0-indexed).
The next n lines each contain m space-separated integers, either 0 or 1, representing the initial state of each cell. 0 means unpainted, 1 means already painted (obstacle).
Output
Print n lines, each containing m space-separated integers, representing the final state of the grid after the paint spreads from the starting position.
On the last line, print the total count of painted cells.
Notes
- Paint spreads only to orthogonally adjacent cells (up, down, left, right), not diagonally.
- If the starting cell is already painted (1), no paint spreads at all.
- The paint spreading is instantaneous—all reachable unpainted cells become painted simultaneously from Suki's perspective.