| 3 | int cols; |
| 4 | int dirs[][] = {{-1,0},{0,1},{1,0},{0,-1}}; |
| 5 | public boolean dfs(int row, int col,int[][] grid1, int[][] grid2, |
| 6 | boolean visited[][]){ |
| 7 | visited[row][col] = true; |
| 8 | boolean isIsland = true; |
| 9 | if(grid1[row][col]==0){ |
| 10 | isIsland = false; |
| 11 | } |
| 12 | //visiting neighbour cells; |
| 13 | for(int dir[] : dirs){ |
| 14 | int nextR = row + dir[0]; |
| 15 | int nextC = col + dir[1]; |
| 16 | if(nextR>=0 && nextC>=0 && nextR<rows && nextC<cols && grid2[nextR][nextC]==1 && !visited[nextR][nextC]){ |
| 17 | boolean res = dfs(nextR,nextC,grid1,grid2,visited); |
| 18 | isIsland = isIsland && res; |
| 19 | } |
| 20 | } |
| 21 | return isIsland; |
| 22 | } |
| 23 | public int countSubIslands(int[][] grid1, int[][] grid2) { |
| 24 | rows = grid1.length; |
| 25 | cols = grid1[0].length; |