4
void mark_current_island(vector<vector<int>> &grid1, vector<vector<int>> &grid2, int x, int y,6
if (x < 0 || x >= r || y < 0 || y >= c || grid2[x][y] != 1) // Boundary case for matrix9
// if there is water on grid1 for the location on B then our current island10
// can't be counted and so we mark res as false11
if (grid1[x][y] == 0) {15
// Mark current cell as visited18
// Make recursive call in all 4 adjacent directions19
mark_current_island(grid1, grid2, x + 1, y, r, c); // DOWN20
mark_current_island(grid1, grid2, x, y + 1, r, c); // RIGHT21
mark_current_island(grid1, grid2, x - 1, y, r, c); // TOP22
mark_current_island(grid1, grid2, x, y - 1, r, c); // LEFT24
int countSubIslands(vector<vector<int>> &grid1, vector<vector<int>> &grid2) {26
ios_base::sync_with_stdio(false);29
int rows = grid1.size();30
if (rows == 0) // Empty grid boundary case32
int cols = grid1[0].size();34
// Iterate for all cells of the array35
int no_of_islands = 0;38
for (int i = 0; i < rows; ++i) {39
for (int j = 0; j < cols; ++j) {40
if (grid2[i][j] == 1) {42
mark_current_island(grid1, grid2, i, j, rows, cols);43
// if current island of grid2 meets the requirement update the no of45
if (res) no_of_islands += 1;