1139. Largest 1-Bordered Square.cpp
Code\n\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {        \n        int r = grid.size();\n        int c = grid[0].si
**Code**\n```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {        \n        int r = grid.size();\n        int c = grid[0].size();\n        int hdp[r][c];\n        int vdp[r][c];        \n        int maxSquareL = 0;\n        int squareL = 0;\n        for(int i=0;i<r;i++)\n        {\n            for(int j=0;j<c;j++)\n            {\n                if(grid[i][j]==1)\n                {\n                    hdp[i][j] = j>0?(1+hdp[i][j-1]):1;\n                    vdp[i][j] = i>0?(1+vdp[i-1][j]):1;                    \n                }\n                else\n                {\n                    hdp[i][j] = 0;\n                    vdp[i][j] = 0;                    \n                }\n                int minL = min(hdp[i][j],vdp[i][j]);\n                if(minL>maxSquareL)\n                {\n                    while(minL>maxSquareL)\n                    {\n                        if(hdp[i+1-minL][j]>=minL && vdp[i][j+1-minL]>=minL)\n                        {\n                            squareL = minL;\n                            break;\n                        }\n                        minL--;\n                    }                    \n                }\n                else\n                    squareL = grid[i][j];\n                \n                if(maxSquareL<squareL)maxSquareL=squareL;\n            }            \n        }        \n        return maxSquareL * maxSquareL;\n    }\n};\n```
['C']