How to Approch this Question ? with Visualization O(N^2)
I am writting this solution by considering You have idea about how to solve 695. Max Area of Island\n\n Step 1 : we will do dfs and give index to each island to
I am writting this solution by considering You have idea about how to solve **695. Max Area of Island**\n\n* **Step 1 :** we will do dfs and give index to each island to distinguish .(simply by one more parameter index).\n* **Step  2 :** then we map index  to area of each island , means area of island corressponding to that index island (Using HashMap)\n* **Step3 :**  then explore all zero\'s and simply find all its neighbour and sum the area of neighbour of zero and find global maximum.\n\nLet Visualize how our matrix look after doing dfs \n\ngrid[] =\n{0,0,**1**,0,0,0,0,**1**,0,0,0,0,0},\n{0,0,0,0,0,0,0,**1,1,1**,0,0,0},\n{0,**1,1**,0,**1**,0,0,0,0,0,0,0,0},\n{0,**1**,0,0,**1,1**,0,0,**1**,0,**1**,0,0},\n{0,**1**,0,0,**1,1**,0,0,**1,1,1**,0,0},\n{0,0,0,0,0,0,0,0,0,0,**1**,0,0},\n{0,0,0,0,0,0,0,**1,1,1**,0,0,0},\n{0,0,0,0,0,0,0,**1,1**,0,0,0,0} \n\n\n\n\n```\nvisited array\n\n0  0  1  0  0  0  0  2  0  0  0  0  0  \n0  0  0  0  0  0  0  2  2  2  0  0  0  \n0  3  3  0  4  0  0  0  0  0  0  0  0  \n0  3  0  0  4  4  0  0  5  0  5  0  0  \n0  3  0  0  4  4  0  0  5  5  5  0  0  \n0  0  0  0  0  0  0  0  0  0  5  0  0  \n0  0  0  0  0  0  0  6  6  6  0  0  0  \n0  0  0  0  0  0  0  6  6  0  0  0  0 \n``` \n```\n\nhashMap =>   {1=1, 2=4, 3=4, 4=5, 5=6, 6=5}\nIndex 1 island size 1 ,index 2 island size 4, index 3 island size 4 ans so on\n\n```\n\n**now we can explore all zeros and  its neighbour , and find overall maximum size island after traversing whole grid**\n\n\n\n```\nSimple 2 more function `\n\n1  dfs(grid,i,j,isVisited,index)\n1.  valid(grid ,,X ,Y)\n```\n\n\n**putting comment for better understanding****\n\n```\n  public static int largestIsland(int[][] grid) {\n        int row  =  grid.length;\n        int col  =  grid[0].length;\n        \n        int isVisited[][]  =  new int[row][col];\n        HashMap<Integer,Integer>map=  new HashMap<>();\n        int maxArea = 0;\n        int index  = 1; // island index to distinguish\n        for(int i=0;i<row;i++){\n            for(int j=0;j<col;j++ ){\n                if(grid[i][j]==1  && isVisited[i][j]<=0 ){\n                \t// maping area to index of that island \n                    map.put(index,dfs(grid,i,j,isVisited,index));\n                    maxArea  =  Math.max(map.get(index),maxArea);\n                    index++;\n                }                \n            }\n            \n        }\n        \n//        Till this we have isVisited matrix containing all insland identified by index , see above visited array\n        \n        for(int i=0;i<row;i++){\n            for(int j=0;j<col;j++ ){\n                \n                if(grid[i][j]==0){\n                \tint area  = 0;\n                  // find all valid neighbour of 0  \n                int dir[][] =  {{0,1},{1,0},{0,-1},{-1,0}};\n                HashSet<Integer> set  =  new HashSet<Integer>();\n                // using hashset to avoid duplicate neighbour\n                 for(int d[]:dir){\n                    int newX =  i+d[0];\n                    int newY =  j+d[1];\n                    if(isValid(grid,newX,newY) ){\n                    set.add(isVisited[newX][newY]);\n                        }                \n                    }\n               \n                 \n                    // then sum area of all neighbour\n                    for(Integer idx :set){\n                   \t\n                        area+= map.getOrDefault(idx,0);\n                    }                    \n                    maxArea  = Math.max(maxArea,area+1);\n                }\n              \n            }\n            \n        }\n        return maxArea;\n    }\n      private static int dfs(int grid[][] , int x,int y, int isVisited[][],int index){\n        // for x,y cell explore all its neighbour                 \n        if(grid[x][y]==0) return 0 ;\n        isVisited[x][y] = index;\n        int dir[][] =  {{0,1},{1,0},{0,-1},{-1,0}};\n        int count  = 1;\n        for(int d[]:dir){\n            int newX =  x+d[0];\n            int newY =  y+d[1];\n            if(isValid(grid,newX,newY) && isVisited[newX][newY] <=0){\n                \n                  count+=dfs(grid,newX,newY,isVisited,index);\n                \n            }\n        }\n     return count;\n    }\n    private static boolean isValid(int grid[][],int x,int y){\n        return  !(x<0 || x>=grid.length || y<0 || y>= grid[0].length);        \n    }\n    \n```
['Depth-First Search', 'Java']