C++ dp with Explanation
After hardcoding for edge cases which include - presence of only zeros, rows  = 1 or columns = 1 we move to the dp part.\nHere I have used 2 dimensional vector 
After hardcoding for edge cases which include - presence of only zeros, rows  = 1 or columns = 1 we move to the dp part.\nHere I have used 2 dimensional vector of pairs. Each element of the vector for example say the pair at the ith row and the jth column is of the form (top, left). Top stores the count of continuous ones above this position in the input array and left stores the count of continuous ones to the left of this position in the input array. Also note that if grid[i][j] = 0 then dp[i][j] = {0,0}\nNow for example an input vector\n1 0 1 1 \n1 1 1 1\n1 1 0 1\n1 1 1 1\nThe dp shall be of the form\n(0,0) (0,0) (0,0) (0,1) \n(1,0) (0,1) (1,2) (1,3) \n(2,0) (1,1) (0,0) (2,0) \n(3,0) (2,1) (0,2) (3,3)\n\nFrom simple observation it is clear that the ans is 9. But how do we reach at this conclusion from this dp array?\nWhat we need to do now is for each dp pair such that both elements are greater than 0 we will take the minimum of the two and if this minimum is say x, then we have to go x boxes up and x boxes left. The pair which is x boxes up must have atleast x number of ones on it\u2019s left and the pair which is x boxes left must have atleast x number of ones on top of it. If it is then calculate it\u2019s area and keep track of the maximum. \nThe trick here is for the input array I wrote above the ans is 9 consisting of the location (3,3)\u2014{zero based indexing} as the bottom right of the square but the minimum of 3 and 3 is 3. So, x = 3, but this doesn\u2019t satisfy our conditions so we need to run a while loop such that it will run from 3 to 1. So, when x becomes 2 we get the ans. \nThe code is:-\n \n \n```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        if(grid.size()==0)\n            return 0;\n        int n = grid.size();\n        int m = grid[0].size();\n        bool flag = false;\n        vector<vector<pair<int,int>>>dp(n, vector<pair<int,int>>(m, {0,0}));\n        for(int i = 0; i<n; i++){\n            for(int j = 0; j<m; j++){\n                if(grid[i][j]==0)\n                    dp[i][j] ={0,0};\n                else{\n                    flag = true;\n                    int left = 0, top = 0;\n                    if(i!=0){\n                        if(grid[i-1][j]==0)\n                            top = 0;\n                        else\n                            top = dp[i-1][j].first+1;\n                    }\n                    if(j!=0){\n                        if(grid[i][j-1]==0)\n                            left = 0;\n                        else\n                            left = dp[i][j-1].second+1;\n                    }\n                    dp[i][j] = {top,left};\n                }\n            }\n        }\n        if(!flag)\n            return 0;\n        if(n==1 || m==1)\n            return 1;\n        int ans = 1;\n        for(int i = 0; i<n; i++){\n            for(int j = 0; j<m; j++){\n                if(dp[i][j].first >0 && dp[i][j].second>0){\n                    int x = min(dp[i][j].first, dp[i][j].second);\n                    while(x){\n                        int top = dp[i-x][j].second;\n                        int left = dp[i][j-x].first;\n                        int mini = min({top, left, x});\n                        if(top>=x && left>=x)\n                            ans = max(ans, (mini+1)*(mini+1));\n                        x--;\n                    }\n                }\n            }\n        }\n        return ans;\n        //return ans;\n    }\n};\n```
[]