# Leetcode 1034 - Coloring a border

`Note:`

- Use
`DFS`

with`visited[][]`

. - Mark cells that should be colored as
`[color, oldColor]`

. - Mark all
`first row`

,`last row`

,`first col`

,`last col`

. - Mark cells whose neighbor has different
`val`

and it’s not an array. - After
`DFS`

, convert`[color, oldColor]`

into`color`

.

`Question:`

You are given an `m x n`

integer matrix grid, and three integers `row`

, `col`

, and `color`

. Each value in the grid represents the color of the grid square at that location.

Two squares belong to the same `connected component`

if they have the same color and are next to each other in any of the 4 directions.

The `border of a connected component`

is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).

You should color the border of the connected component that contains the square `grid[row][col]`

with `color`

.

Return the final `grid`

.

`Example:`

1 | Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 |

`Code:`

1 | /** |