Flood fill

image is a list of lists of whole numbers (colours). Write flood_fill(image, r, c, colour) that changes the cell at (r, c) and every cell connected to it (up, down, left, right) through cells of the same original colour to the new colour, in place, and returns image.

flood_fill([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 2)  ->  [[2, 2, 2], [2, 2, 0], [2, 0, 1]]
flood_fill([[0, 0]], 0, 0, 0)                          ->  [[0, 0]]   (same colour: nothing changes)

The point is to practise a search on a grid that stops at a colour boundary. Watch the case where the new colour equals the old one, which loops forever if handled naively.

def flood_fill(image, r, c, colour):
    ...