C++ DP Solution
\nstruct Co{\n   int x;\n   int y;    \n}; \nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m=grid.size(); 
```\nstruct Co{\n   int x;\n   int y;    \n}; \nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m=grid.size(); \n        int n=grid[0].size();  \n        \n        vector<vector<Co>>dp(m,vector<Co>(n,{0,0}));\n        if(grid[0][0]){\n            dp[0][0]={1,1};  \n        }\n        for(int i=1;i<m;i++){\n            if(grid[i][0]!=0){\n                dp[i][0]={1+dp[i-1][0].x,1}; \n            }\n        }\n        \n        for(int j=1;j<n;j++){\n            if(grid[0][j]!=0){\n                dp[0][j]={1,dp[0][j-1].y+1}; \n            }\n        }\n        \n        for(int i=1;i<m;i++){\n            for(int j=1;j<n;j++){\n              if(grid[i][j]!=0)  dp[i][j]={dp[i-1][j].x+1,dp[i][j-1].y+1};  \n            }\n        }\n        int ans=0;  \n        for(int i=m-1;i>=0;i--){\n            for(int j=n-1;j>=0;j--){\n               int mini; \n               if(dp[i][j].x) mini= min(dp[i][j].x,dp[i][j].y);  \n               else continue; \n                mini--;  \n                while(mini>=0){\n                  if(  dp[i-mini][j].y>=mini and dp[i][j-mini].x>=mini and grid[i-mini][j-mini]==1){\n                      ans=max(ans,mini+1); \n                      break; \n                  }\n                  mini--;  \n                  \n                }\n                \n            }\n        }\n        ans=ans*ans; \n        return ans; \n    }\n};\n```
['Dynamic Programming']