C++ || DFS || FASTER THAN 95%
Try to understand the use of id and how we are using it to map the count\'s val.\n\nclass Solution {\npublic:\n    vector<pair<int,int>>dir={{1,0},{0,1},{-1,0},
Try to understand the use of id and how we are using it to map the count\'s val.\n```\nclass Solution {\npublic:\n    vector<pair<int,int>>dir={{1,0},{0,1},{-1,0},{0,-1}};\n    bool check(int i,int j,vector<vector<int>>& grid){\n        if(i<grid.size()&&j<grid[0].size()&&i>=0&&j>=0&&grid[i][j]==1)return true;\n        return false;\n    }\n    int dfs(vector<vector<int>>&grid,int i,int j,int&val,vector<vector<int>>&vis){\n        int count=1;\n        vis[i][j]=val;\n        for(auto x:dir){\n            if(check(i+x.first,j+x.second,grid)&&vis[i+x.first][j+x.second]!=val){\n               count += dfs(grid,i+x.first,j+x.second,val,vis); \n            }\n        }\n        return count;\n    }\n    int largestIsland(vector<vector<int>>& grid) {\n        int val=1;\n        int n=grid.size();\n        int m=grid[0].size();\n        vector<vector<int>>vis(n+1,vector<int>(m+1));\n        vector<int>dp(n*m+2);\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(grid[i][j]==1&&vis[i][j]==0){\n                    dp[val]=dfs(grid,i,j,val,vis);\n                    val++;\n                }\n            }\n        }\n        int ans=0;\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(vis[i][j]==1){\n                    ans=max(ans,dp[vis[i][j]]);\n                }else{\n                    int u=0,d=0,l=0,r=0,ui=0,di=0,li=0,ri=0;\n                    if(i>0){\n                        ui=vis[i-1][j];\n                        u=dp[vis[i-1][j]];\n                    }\n                    if(i<n-1){\n                        di=vis[i+1][j];\n                        if(ui!=di)d=dp[vis[i+1][j]];\n                    }\n                    if(j>0){\n                        li=vis[i][j-1];\n                        if(li!=ui&&li!=di)l=dp[vis[i][j-1]];\n                    }\n                    if(j<m){\n                        ri=vis[i][j+1];\n                        if(ri!=li&&ri!=ui&&ri!=di)r=dp[vis[i][j+1]];\n                    }\n                    ans=max(ans,r+l+u+d+1);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
[]