Explained and Commented Solution
\n\nclass Solution {\npublic:\n     int findLargestSquare(vector<vector<int>>& mat) \n    { \n    int max = 0; int m = mat.size() , n = mat[0].size();\n    vect
\n```\nclass Solution {\npublic:\n     int findLargestSquare(vector<vector<int>>& mat) \n    { \n    int max = 0; int m = mat.size() , n = mat[0].size();\n    vector<vector<int>> hor(m,vector<int> (n,0)) , ver(m,vector<int> (n,0));\n   \n    for (int i=0; i<m; i++) { \n        for (int j=0; j<n; j++) { \n            if (mat[i][j] == 1) \n            { \n                hor[i][j] = (j==0)? 1: hor[i][j-1] + 1;   // auxillary horizontal array\n                ver[i][j] = (i==0)? 1: ver[i-1][j] + 1;  // auxillary vertical array\n            } \n        } \n    } \n        \n    for (int i = m-1; i>=0; i--) { \n        for (int j = n-1; j>=0; j--) { \n            int small = min(hor[i][j], ver[i][j]);  // choose smallest of horizontal and vertical value\n            while (small > max) { \n                if (ver[i][j-small+1] >= small &&  hor[i-small+1][j] >= small)  // check if square exists with \'small\' length\n                    max = small; \n                small--; \n            } \n        } \n    } \n    return max*max; \n} \n    \n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        return findLargestSquare(grid); \n    }\n};\n```
['C++']