public void apply(int[][] picture, int colorToReplace, int colorToPaint, int x, int y) {
    validatePicture(picture);

    int currentColor = getValueAt(picture, x, y);
    if (currentColor == colorToReplace) {
      picture[x][y] = colorToPaint;
      apply(picture, colorToReplace, colorToPaint, x + 1, y);
      apply(picture, colorToReplace, colorToPaint, x - 1, y);
      apply(picture, colorToReplace, colorToPaint, x, y + 1);
      apply(picture, colorToReplace, colorToPaint, x, y - 1);
    }
  }
Implementation of a tail recursive flood fill algorithm to solve this problem. Flood fill,
also called seed fill, is an algorithm that determines the area connected to a given node in a
multi-dimensional array. It is used in the "bucket" fill tool of paint programs to fill
connected, similarly-colored areas with a different color, and in games such as Go and
Minesweeper for determining which pieces are cleared. When applied on an image to fill a
particular bounded area with color, it is also known as boundary fill.

The complexity order of this algorithm in space terms is O(1) because we are not using any
additional data structure to store data temporally. In time termsn the complexity order is
O(N) where N is the number of pixels to change color.
src/main/java/com/github/pedrovgs/problem42/FloodFill.java