C++ DFS Solution || Assign ID's to island || commented & exlained
\nclass Solution {\npublic:\n    //we use the concept of leetcode 695- maximum area of island\n    //we assign all connected islands with different id\'s \n    
```\nclass Solution {\npublic:\n    //we use the concept of leetcode 695- maximum area of island\n    //we assign all connected islands with different id\'s \n    //we do it inplace\n    //we then store the area of the island id in a hashmap\n    \n    //we again iterate through the isalnd and when we have zero\n    //we push all the 4 adjacent cell\'s island id to hashset to get the unique id\'s\n    //the we calculate the sum of the area of each id in hashset and update the result\n    vector<int> offsets={0,1,0,-1,0};\n    int largestIsland(vector<vector<int>>& grid) {\n        \n        int n=grid.size();\n        unordered_map<int,int> area;\n        \n        int maxArea=0;\n        int currArea=0;\n        int island_id=2;\n        \n        for(int i=0;i<n;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(grid[i][j]==1)\n                {\n                    \n                    calc(grid,i,j,currArea,n,island_id);\n                    maxArea=max(maxArea,currArea);\n                    //store the area of the island using its id\n                    area[island_id]=currArea;\n                    currArea=0;\n                    island_id++;\n                }\n            }\n        }\n        \n        for(int i=0;i<n;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(grid[i][j]==0)\n                {\n                    unordered_set<int> st;\n                    for(int k=0;k<4;k++)\n                    {\n                        int x=i+offsets[k];\n                        int y=j+offsets[k+1];\n                        if(x<0 || y<0 || x>=n || y>=n || grid[x][y]==0)\n                            continue;\n                        st.insert({grid[x][y]});\n                    }\n                    \n                    int total=1;\n                    for(auto i:st){\n                        total+=area[i];\n                    \n                    }\n                    maxArea=max(maxArea,total);\n                }\n            }\n        }\n        \n        return maxArea;\n        \n        \n        \n    }\n\t\n\t//calculates the area of island\n    void calc(vector<vector<int>>& grid,int i,int j,int& currArea,int& n,int& island_id)\n    {\n        currArea++;\n        //store the island id to which the current cell belongs to\n        grid[i][j]=island_id;\n        for(int k=0;k<4;k++)\n        {\n            int x=i+offsets[k];\n            int y=j+offsets[k+1];\n            if(x<0 || y<0 || x>=n || y>=n || grid[x][y]!=1)\n                continue;\n            calc(grid,x,y,currArea,n,island_id);\n        }\n        \n        \n    }\n};\n```\n**Upvote if this helps you :)**
['C']