C++ | Dynamic programming
\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int r = grid.size();\n        int c = grid[0].size();\n       
```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int r = grid.size();\n        int c = grid[0].size();\n        vector<vector<pair<int,int>>> dp(r, vector<pair<int,int>>(c,{0,0}));\n        \n        int mx = 0;\n\n        for(int i=0;i<r;i++){\n            for(int j=0;j<c;j++){\n                if(grid[i][j]!=0){\n                    \n                    dp[i][j].first = i == 0 ? 1 : dp[i-1][j].first+1;\n                    dp[i][j].second = j == 0 ? 1 : dp[i][j-1].second+1;\n                    \n                    int sz = min(dp[i][j].first, dp[i][j].second);\n                    while(sz>mx){\n                        int p = min(dp[i-sz+1][j].second,dp[i][j-sz+1].first);\n                        if(p>=sz)\n                            mx = max(mx,sz);\n                        sz--;\n                    }\n                }\n            }\n        }\n            \n        return mx*mx;\n    }\n};\n```
['Dynamic Programming', 'C++']