Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

&nbsp;
Example 1:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1


Example 2:

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	1 &lt;= m, n &lt;= 300
	grid[i][j] is '0' or '1'.


seen=set()
def dfs(i,j,m,n,grid):
    global seen
    if (i<0 or i>=m or j<0 or j>=n):return
    if (i,j) in seen:return
    seen.add((i,j))
    if grid[i][j]=="1":
        dfs(i,j+1,m,n,grid)
        dfs(i,j-1,m,n,grid)
        dfs(i+1,j,m,n,grid)
        dfs(i-1,j,m,n,grid)

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        global seen
        m=len(grid) #no of rows
        n=len(grid[0]) #no of columns
        count=0
        for i in range(m):
            for j in range(n):
                if (i,j) not in seen and grid[i][j]=="1":
                    dfs(i,j,m,n,grid)
                    count+=1
        seen.clear()
        return count
class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        int r = grid.length;
        int c = grid[0].length;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(grid[i][j]=='1'){
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }
    public void dfs(char[][] grid,int i,int j){
        if(i<0 || j<0 || i>=grid.length || j>=grid[0].length || grid[i][j]=='0'){
            return;
        }
        grid[i][j] = '0';
        dfs(grid,i,j+1);
        dfs(grid,i,j-1);
        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
    }
}
class Solution {
public:

    void calculate(vector<vector<char>>& grid, int i, int j)
    {
        if(i>=grid.size() || j>=grid[i].size() || i<0 || j<0)
            return;

        if(grid[i][j]=='0')
            return;

        grid[i][j] = '0';
        calculate(grid,i,j-1);
        calculate(grid,i-1,j);
        calculate(grid,i,j+1);
        calculate(grid,i+1,j);

    }

    int numIslands(vector<vector<char>>& grid) {

        int ans = 0;

        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                if(grid[i][j]=='0')
                    continue;

                else if (grid[i][j]=='1')
                {
                    ans++;
                    calculate(grid,i,j);
                }
            }
        }
        return ans;
    }
};
/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    const directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    let islandCount = 0
    
    for (let rowIndex = 0; rowIndex < grid.length; rowIndex++) {
        for (let colIndex = 0; colIndex < grid[0].length; colIndex++) {
            const node = grid[rowIndex][colIndex]
            
            // Don't bother with water
            if (node === "0") continue
            
            // Once we encounter land, try to get all
            // the connected land from the current land.
            // Once we get all the land connected from
            // the current land, mark them as water so
            // that it would disregarded by the main loop.
            islandCount++
            
            const connectedLandStack = [[rowIndex, colIndex]]
            
            while(connectedLandStack.length > 0) {
                const [row, col] = connectedLandStack.pop()
                
                // change the land to water
                grid[row][col] = "0"
                
                for (const direction of directions) {
                    // To get all the connected land we need to check
                    //  all directions (left, right, top, bottom)
                    const newNode = [row + direction[0], col + direction[1]]
                    const newNodeValue = grid[newNode[0]]?.[newNode[1]]
                    
                    // We only care about the connected lands
                    if (newNodeValue === undefined || newNodeValue === "0") {
                        continue
                    }
                    
                    connectedLandStack.push(newNode)
                }
            }
        }
    }
    
    return islandCount
};
Number of Islands