# Leetcode 1020. Number of Enclaves

`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: 3Explanation: 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.`
`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.`
`class Solution {public:    int rsize{0};    int csize{0};    int count{0};    vector<vector<int>> next{{1,0}, {-1,0}, {0,1}, {0,-1}};        int dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, vector<vector<bool>>& visited, int r, int c, bool& sea) {        if (r == rsize || r < 0 || c == csize || c < 0 || visited[r][c] || grid2[r][c] == 0) {            return 0;        }                     if (grid1[r][c] == 0) {            sea = true;        }visited[r][c] = true;        for (int i=0; i<4; i++) {            dfs(grid1, grid2, visited, r+next[i], c+next[i], sea);        }        if (sea)            return 0;                return 1;         }        int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {        rsize = grid1.size();        csize = grid1.size();                 vector<vector<bool>> visited(rsize, vector<bool>(csize, false));        for (int i=0; i<rsize; i++) {            for (int j=0; j<csize; j++) {                if (grid1[i][j]) {                    bool sea = false;                    count += dfs(grid1, grid2, visited, i, j, sea);                }            }        }        return count;                  }};`

--

--