Beats 100%🔥🔥 || Hard❌ Easy✅ Simple Logic🔥 with Explanation👌👌!!!
Here the Qusestion requests us to do two tasks >>\n\nTask 1:(Calculate the islands and count ):\n\n. In this task we would be calculating the islands and their 
Here the Qusestion requests us to do two tasks >>\n\n**Task 1:**(Calculate the islands and count ):\n\n. In this task we would be calculating the islands and their respective area and storing them in a hashMap for further reference.this could be done using simple dfs.\n\n**Task 2:**(calculate the largest area possible):\n\n. In this task we will be iterating through all the cells containing water ```(0)``` and try changing them into land and check all the four sides contains any piece of land. if yes,then we would take the max of it and return. we will also check whether a chink of island is used only once not multiple times.\n\nThe provided snippet is a overall overview of [MAX AREA OF ISLAND](https://leetcode.com/problems/max-area-of-island/description/) try to solve this problem before attempting this.\n\n\n# Code\n``` Java []\nclass Solution {\n    public int largestIsland(int[][] grid) {\n        int n=grid.length;\n        int ref=2,max=-1;\n        HashMap<Integer,Integer> mp=new HashMap<>();\n\n        //MAX AREA OF ISLAND\n        for(int i=0;i<n;i++){\n            for(int j=0;j<n;j++){\n                if(grid[i][j]==1) {\n                    int k=find_val(ref,grid,i,j);\n                    mp.put(ref,k);\n                    ref+=1;\n                    max=Math.max(max,k);\n                }\n            }\n\n        //MAX AREA ALONG WITH THE ADDITION OF ONE PIECE OF LAND.\n        mp.put(0,0);\n        for(int i=0;i<n;i++){\n            for(int j=0;j<n;j++){\n                if(grid[i][j]==0) {\n                    int sum=0,left=0,right=0,up=0,down=0;\n                    if(i>0){\n                        sum+=(int)mp.get(grid[i-1][j]);\n                        up=grid[i-1][j];\n                    }\n                    if(j>0&&grid[i][j-1]!=up) {\n                        sum+=(int)mp.get(grid[i][j-1]);\n                        left=grid[i][j-1];\n                    }\n                    if(i<n-1&&grid[i+1][j]!=up&&grid[i+1][j]!=left){\n                        sum+=(int)mp.get(grid[i+1][j]);\n                        down=grid[i+1][j];\n                    }\n                    if(j<n-1&& grid[i][j+1]!=up&& grid[i][j+1]!=down&&grid[i][j+1]!=left){\n                        sum+=(int)mp.get(grid[i][j+1]);\n                    }\n                    max=Math.max(max,sum+1);\n                }\n            }\n        }\n        return max;\n    }\n\n// MAX AREA OF ISLAND\n    public static int find_val(int ref,int[][] arr,int i,int j){\n        if(i<0||j<0||i>=arr.length||j>=arr[0].length||arr[i][j]==0||arr[i][j]==ref){\n            return 0;\n        }\n        arr[i][j]=ref;\n        return 1+((find_val(ref,arr,i+1,j)+find_val(ref,arr,i-1,j)+find_val(ref,arr,i,j+1)+find_val(ref,arr,i,j-1)));\n    }\n}\n```\n``` Python3 []\nclass Solution:\n    def largestIsland(self, grid: List[List[int]]) -> int:\n        def find_val(ref, grid, i, j):\n            if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0 or grid[i][j] == ref:\n                return 0\n            grid[i][j] = ref\n            return 1 + (find_val(ref, grid, i+1, j) + find_val(ref, grid, i-1, j) + find_val(ref, grid, i, j+1) + find_val(ref, grid, i, j-1))\n\n        n = len(grid)\n        ref = 2\n        max_area = -1\n        mp = {}\n\n        for i in range(n):\n            for j in range(n):\n                if grid[i][j] == 1:\n                    k = find_val(ref, grid, i, j)\n                    mp[ref] = k\n                    ref += 1\n                    max_area = max(max_area, k)\n\n        mp[0] = 0\n        for i in range(n):\n            for j in range(n):\n                if grid[i][j] == 0:\n                    sum_area = 0\n                    seen = set()\n                    if i > 0:\n                        seen.add(grid[i-1][j])\n                    if j > 0:\n                        seen.add(grid[i][j-1])\n                    if i < n-1:\n                        seen.add(grid[i+1][j])\n                    if j < n-1:\n                        seen.add(grid[i][j+1])\n                    for k in seen:\n                        sum_area += mp.get(k, 0)\n                    max_area = max(max_area, sum_area + 1)\n\n        return max_area\n\n```\n``` C++ []\nclass Solution {\npublic:\n    int largestIsland(vector<vector<int>>& grid) {\n        int n = grid.size();\n        int ref = 2, maxArea = -1;\n        unordered_map<int, int> mp;\n\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 1) {\n                    int k = find_val(ref, grid, i, j);\n                    mp[ref] = k;\n                    ref += 1;\n                    maxArea = max(maxArea, k);\n                }\n            }\n        }\n\n        mp[0] = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 0) {\n                    int sum = 0;\n                    unordered_set<int> seen;\n                    if (i > 0) seen.insert(grid[i-1][j]);\n                    if (j > 0) seen.insert(grid[i][j-1]);\n                    if (i < n-1) seen.insert(grid[i+1][j]);\n                    if (j < n-1) seen.insert(grid[i][j+1]);\n                    for (int val : seen) {\n                        sum += mp[val];\n                    }\n                    maxArea = max(maxArea, sum + 1);\n                }\n            }\n        }\n        return maxArea;\n    }\n\n    int find_val(int ref, vector<vector<int>>& grid, int i, int j) {\n        if (i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] == 0 || grid[i][j] == ref) {\n            return 0;\n        }\n        grid[i][j] = ref;\n        return 1 + find_val(ref, grid, i+1, j) + find_val(ref, grid, i-1, j) + find_val(ref, grid, i, j+1) + find_val(ref, grid, i, j-1);\n    }\n};\n\n```\n``` JavaScript []\nvar largestIsland = function(grid) {\n    const find_val = (ref, grid, i, j) => {\n        if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] === 0 || grid[i][j] === ref) {\n            return 0;\n        }\n        grid[i][j] = ref;\n        return 1 + (find_val(ref, grid, i + 1, j) + find_val(ref, grid, i - 1, j) + find_val(ref, grid, i, j + 1) + find_val(ref, grid, i, j - 1));\n    };\n\n    const n = grid.length;\n    let ref = 2, maxArea = -1;\n    const mp = new Map();\n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            if (grid[i][j] === 1) {\n                const k = find_val(ref, grid, i, j);\n                mp.set(ref, k);\n                ref += 1;\n                maxArea = Math.max(maxArea, k);\n            }\n        }\n    }\n\n    mp.set(0, 0);\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            if (grid[i][j] === 0) {\n                let sum = 0;\n                const seen = new Set();\n                if (i > 0) seen.add(grid[i - 1][j]);\n                if (j > 0) seen.add(grid[i][j - 1]);\n                if (i < n - 1) seen.add(grid[i + 1][j]);\n                if (j < n - 1) seen.add(grid[i][j + 1]);\n                seen.forEach(val => sum += mp.get(val));\n                maxArea = Math.max(maxArea, sum + 1);\n            }\n        }\n    }\n    return maxArea;\n};\n\n```
['Array', 'Depth-First Search', 'C++', 'Java', 'Python3', 'JavaScript']