You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

Return the number of islands in grid2 that are considered sub-islands.

&nbsp;
Example 1:

Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.


Example 2:

Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2 
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.


&nbsp;
Constraints:


	m == grid1.length == grid2.length
	n == grid1[i].length == grid2[i].length
	1 &lt;= m, n &lt;= 500
	grid1[i][j] and grid2[i][j] are either 0 or 1.


class Solution(object):
    def countSubIslands(self, grid1, grid2):
        m=len(grid1)
        n=len(grid1[0])
        
        def dfs(i,j):
            if i<0 or i>=m or j<0 or j>=n or grid2[i][j]==0:
                return 
            grid2[i][j]=0
            dfs(i+1,j)
            dfs(i-1,j)
            dfs(i,j+1)
            dfs(i,j-1)
        # here we remove the unnecesaary islands by seeing the point that if for a land in grid2 and water in grid1 it cannot be a subisland and hence island in which this land resides should be removed 
        for i in range(m):
            for j in range(n):
                if grid2[i][j]==1 and grid1[i][j]==0:
                    dfs(i,j)
		#now we just need to count the islands left over 			
        count=0
        for i in range(m):
            for j in range(n):
                if grid2[i][j]==1:
                    
                    dfs(i,j)
                    count+=1
        return count 
		
class Solution {
    public int countSubIslands(int[][] grid1, int[][] grid2) {
        int m = grid1.length;
        int n = grid1[0].length;
        boolean[][] vis = new boolean[m][n];
        int count = 0;
        int[] dir = {1, 0, -1, 0, 1};

        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                if(grid2[i][j] == 0 || vis[i][j])
                    continue;

                Queue<int[]> queue = new LinkedList<>();
                boolean flag = true;
                vis[i][j] = true;

                queue.add(new int[] {i, j});

                while(!queue.isEmpty()) {
                    int[] vtx = queue.remove();

                    if(grid1[vtx[0]][vtx[1]] == 0)
                        flag = false;

                    for(int k = 0; k < 4; ++k) {
                        int x = vtx[0] + dir[k];
                        int y = vtx[1] + dir[k + 1];

                        if(x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] == 1 && !vis[x][y]) {
                            vis[x][y] = true;

                            queue.add(new int[] {x, y});
                        }
                    }
                }

                if(flag)
                    ++count;
            }
        }

        return count;
    }
}
class Solution {
public:
    bool res;
    void mark_current_island(vector<vector<int>>& grid1, vector<vector<int>>& grid2,int x,int y,int r,int c){
        if(x<0 || x>=r || y<0 || y>=c || grid2[x][y]!=1)  //Boundary case for matrix
            return ;
        
        //if there is water on  grid1 for the location on B then our current island can't be counted and so we mark res as false 
        if(grid1[x][y]==0){
            res=false;
            return;
        }
        //Mark current cell as visited
        grid2[x][y] = 0;
        
        //Make recursive call in all 4 adjacent directions
        mark_current_island(grid1,grid2,x+1,y,r,c);  //DOWN
        mark_current_island(grid1,grid2,x,y+1,r,c);  //RIGHT
        mark_current_island(grid1,grid2,x-1,y,r,c);  //TOP
        mark_current_island(grid1,grid2,x,y-1,r,c);  //LEFT
    }
    int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
        //For FAST I/O
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int rows = grid1.size();
        if(rows==0)     //Empty grid boundary case
            return 0;
        int cols = grid1[0].size();
		
        //Iterate for all cells of the array
        int no_of_islands = 0;
		
        //dfs on grid2 
        for(int i=0;i<rows;++i){
            for(int j=0;j<cols;++j){
                if(grid2[i][j]==1){
                    res=true;
                    mark_current_island(grid1,grid2,i,j,rows,cols);
                    //if current island of grid2 meets the requirement update the no of islands
                    if(res)
                        no_of_islands += 1;
                }
            }
        }
		
        return no_of_islands;
    }
};
var countSubIslands = function(grid1, grid2) {
    const R = grid2.length, C = grid2[0].length;
    
    // returns no of cells in grid 2 not covered in grid1
    function noOfNotCoveredDfs(i, j) {
        if (i < 0 || j < 0) return 0;
        if (i >= R || j >= C) return 0;
        if (grid2[i][j] !== 1) return 0;
        
        // mark visited
        grid2[i][j] = 2;
        
        return (grid1[i][j] === 1 ? 0 : 1) + 
            noOfNotCoveredDfs(i - 1, j) + 
            noOfNotCoveredDfs(i + 1, j) + 
            noOfNotCoveredDfs(i, j - 1) + 
            noOfNotCoveredDfs(i, j + 1);
    }
    
    let ans = 0;
    for (let i = 0; i < R; i++) {
        for (let j = 0; j < C; j++) {
            if (grid2[i][j] === 1) {
                if (noOfNotCoveredDfs(i, j) === 0) {
                    ans++;
                }
            }
        }
    }
    
    return ans;
};
Count Sub Islands