Easy to understand C++ Solution with Explanation
\'hor\' count horizontal consecutive 1s & \'ver\' count vertical consecutive 1s for each position.\n* Then from bottom to top we check what is the maximum size 
* \'hor\' count horizontal consecutive 1s & \'ver\' count vertical consecutive 1s for each position.\n* Then from bottom to top we check what is the maximum size square we can achieve from the current location.\n\n```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int result=0;\n        int m=grid.size(),n=grid[0].size();\n        vector<vector<int>> hor(m,vector<int>(n,0)); \n        vector<vector<int>> ver(m,vector<int>(n,0));\n        \n        \n        for(int i=0;i<m;i++){\n            for(int j=0;j<n;j++){\n                if(j==0 && grid[i][j]==1) ver[i][j]=1;\n                else if(j!=0 && grid[i][j]==1) ver[i][j]=ver[i][j-1]+1;\n                \n                if(i==0 && grid[i][j]==1) hor[i][j]=1;\n                else if(i!=0 && grid[i][j]==1) hor[i][j]=hor[i-1][j]+1;\n            }\n        }\n        \n        \n        for(int i=m-1;i>=0;i--){\n            for(int j=n-1;j>=0;j--){\n                int sqr=min(ver[i][j],hor[i][j]);\n                \n                while(sqr>result){\n                    if(ver[i-sqr+1][j]>=sqr && hor[i][j-sqr+1]>=sqr) result=sqr;\n                    sqr--;\n                }\n            }\n        }\n        \n        return result*result; \n        \n    }\n};\n```
[]