class FloodFill {

  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);
    }
  }

}