#K68447. Coloring Connected Component in a Matrix

    ID: 32866 Type: Default 1000ms 256MiB

Coloring Connected Component in a Matrix

Coloring Connected Component in a Matrix

You are given an n x m matrix of positive integers and a starting coordinate (sx, sy). Your task is to replace the value of the cell at (sx, sy) and every cell 8-directionally connected to it with 0. Two cells are considered connected if they are adjacent horizontally, vertically, or diagonally. In other words, for a cell at position (x, y), the possible moves are given by the set of directions:

$$\{(-1,0),\,(1,0),\,(0,-1),\,(0,1),\,(-1,-1),\,(-1,1),\,(1,-1),\,(1,1)\}$$

Perform the coloring using a depth-first search (DFS) approach. After the process, output the resulting matrix.

inputFormat

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

n m
row1 (m integers separated by spaces)
row2
...
rown
sx sy

Here, n and m denote the number of rows and columns of the matrix, respectively. The next n lines describe the matrix. The final line contains two integers sx and sy, which represent the starting cell's row and column indices (0-indexed).

outputFormat

Output the modified matrix to stdout. Each of the n lines should contain m integers separated by a single space.

## sample
4 4
1 1 2 3
1 1 2 3
3 3 3 3
4 4 4 4
1 1
0 0 2 3

0 0 2 3 3 3 3 3 4 4 4 4

</p>