
One of the biggest time savers when you draw on a computer is the paint bucket, also called bucket fill.
When you pick this tool and click a pixel of the image, every pixel that has the same color as the clicked pixel and is connected to it gets painted with the new color. Two pixels are connected when they share a side, or when a path of connected pixels joins them.
Look at the picture below. If you click the orange pixel in the center, the whole region turns orange. Pixels are not connected diagonally, so two corners of the image stay white.

You are given a matrix of digits that represents the pixels. Simulate a bucket fill applied to one given pixel. A color is a single digit from 0 to 9.