✅C++ O(n ^ 3)✅✅
AUTHOR: JAYESH BADGUJAR\n\n\n# Code\n\nclass Solution {\npublic: \n    //dp_0[i][0]=horizontal\n    //dp_0[i][1]=vertical\n    bool isValid(int i,int j,int n,in
# AUTHOR: JAYESH BADGUJAR\n\n\n# Code\n```\nclass Solution {\npublic: \n    //dp_0[i][0]=horizontal\n    //dp_0[i][1]=vertical\n    bool isValid(int i,int j,int n,int m){\n        if(i>=0 && i<n && j>=0 && j<m){\n            return true;\n        }\n        return false;\n    }\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int n=grid.size(),m=grid[0].size();\n        vector<vector<vector<int>>> dp(n,vector<vector<int>>(m,vector<int>(2,0)));\n        for(int i=0;i<n;i++){\n            if(grid[i][0]==1){\n                dp[i][0][0]=1;\n                if(i>0){\n                    dp[i][0][1]=1+dp[i-1][0][1];\n                }else{\n                    dp[i][0][1]=1;\n                }\n            }\n        }\n        for(int i=0;i<m;i++){\n            if(grid[0][i]==1){\n                if(i>0){\n                    dp[0][i][0]=1+dp[0][i-1][0];\n                }else{\n                    dp[0][i][0]=1;\n                }\n                dp[0][i][1]=1;\n            }\n        }\n        \n        for(int i=1;i<n;i++){\n            for(int j=1;j<m;j++){\n                if(grid[i][j]==1){\n                    dp[i][j][0]=1+dp[i][j-1][0];\n                    dp[i][j][1]=1+dp[i-1][j][1];\n                   \n                }\n            }\n        }\n        \n        int maxi=0;\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                for(int k=0;k<min(n,m);k++){\n                    if(isValid(i-k,j,n,m) && isValid(i,j-k,n,m) && grid[i][j]==1){\n                         \n                        if(dp[i][j][0]>k && dp[i][j][1]>k && dp[i][j-k][1]>k && dp[i-k][j][0]>k){\n                            int size=(k+1)*(k+1);\n                            maxi=max(maxi,size);\n                        }\n                    }\n                }\n            }\n        }\n        return maxi;\n    }\n};\n```
['C++']