An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].

To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with color.

Return the modified image after performing the flood fill.

&nbsp;
Example 1:

Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.


Example 2:

Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation: The starting pixel is already colored 0, so no changes are made to the image.


&nbsp;
Constraints:


	m == image.length
	n == image[i].length
	1 &lt;= m, n &lt;= 50
	0 &lt;= image[i][j], color &lt; 216
	0 &lt;= sr &lt; m
	0 &lt;= sc &lt; n


class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
        queue = deque()
        rows = len(image)
        cols = len(image[0])
        
        targetColor = image[sr][sc]
        
        if color == targetColor:
		    # in this case, we don't need to do anything
            return image

        rDirs = [1, 0, -1, 0]
        cDirs = [0, 1, 0, -1]
        
        queue.append((sr, sc))
        
        while len(queue) > 0:
            r, c = queue.pop()
            
            image[r][c] = color
            for rd, cd in zip(rDirs, cDirs):
                newRow = r + rd
                newCol = c + cd
                
                isValidCoordinate = newRow >= 0 and newRow < rows and newCol >= 0 and newCol < cols
                
                if isValidCoordinate and image[newRow][newCol] == targetColor:
                    queue.append((newRow, newCol))
        
        return image
            
    
class Solution {
    void colorFill(int[][]image,int sr,int sc,int sourceColor,int targetColor){
        int m = image.length, n = image[0].length;

        if(sr>=0 && sr<m && sc>=0 && sc<n)
        {
            if( (image[sr][sc] != sourceColor) ||(image[sr][sc] == targetColor)) return;

        image[sr][sc] = targetColor;
        colorFill(image,sr,sc-1,sourceColor,targetColor); // left
        colorFill(image,sr,sc+1,sourceColor,targetColor); // right
        colorFill(image,sr-1,sc,sourceColor,targetColor); // up
        colorFill(image,sr+1,sc,sourceColor,targetColor); // down
        }
       else
           return;
    }
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int rows = image.length, cols = image[0].length;

        colorFill(image,sr,sc,image[sr][sc],color);
    return image;
    }
}
class Solution {
public:
    vector<vector<int>> paths = {{0,1},{0,-1},{-1,0},{1,0}};
    bool check(int i,int j , int n, int m){
        if(i>=n or i<0 or j>=m or j<0) return false;
        return true;
    }
    void solve(vector<vector<int>> &image, int sr, int sc, int color, int orig){
        int n = image.size(), m = image[0].size();
        image[sr][sc] = color;
        for(int i=0;i<4;i++){
            int new_sr = paths[i][0] + sr;
            int new_sc = paths[i][1] + sc;
            if(check(new_sr,new_sc,n,m)==true and image[new_sr][new_sc]==orig){
                solve(image, new_sr, new_sc, color,orig);
            }
        }

    }
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if(color==image[sr][sc]) return image;
        int orig = image[sr][sc];
        solve(image, sr,sc,color, orig);
        return image;
    }
};
/**
 * @param {number[][]} image
 * @param {number} sr
 * @param {number} sc
 * @param {number} color
 * @return {number[][]}
 */
var floodFill = function(image, sr, sc, color) {
    const pixelsToCheck = [[sr, sc]]
    const startingPixelColor = image[sr][sc]
    const directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    const seenPixels = new Set()

    if (
        startingPixelColor === undefined ||
        startingPixelColor === color
    ) return image

    for (const pixel of pixelsToCheck) {
        const currentPixelColor = image[pixel[0]]?.[pixel[1]]

        if (
            currentPixelColor === undefined ||
            startingPixelColor !== currentPixelColor
        ) continue

        image[pixel[0]][pixel[1]] = color

        for (const direction of directions) {
            const newPixel = [pixel[0] + direction[0], pixel[1] + direction[1]]
            const pixelString = newPixel.join()

            if (seenPixels.has(pixelString)) continue

            pixelsToCheck.push(newPixel)
            seenPixels.add(pixelString)
        }
    }

    return image
};
Flood Fill