C++, beats 100% (both time and memory)
\nclass Solution {\npublic:\n    Solution() {ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}\n    \n    int largest1BorderedSquare(
```\nclass Solution {\npublic:\n    Solution() {ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}\n    \n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        int top[m][n], left[m][n]; //grid[i][j]\u4E3A\u4E2D\u5FC3\uFF0C\u5411\u4E0A\u5411\u5DE6\u5947\u65701\u7684\u4E2A\u6570\n        for(int i=0; i<m; ++i)for(int j=0; j<n; ++j){\n             top[i][j] = grid[i][j] ? (i>0 ?  top[i-1][j]+1 : 1) : 0;\n            left[i][j] = grid[i][j] ? (j>0 ? left[i][j-1]+1 : 1) : 0;\n        }\n        vector<vector<int>> dp(grid);\n        for(int i=1; i<m; ++i) for(int j=1; j<n; ++j){\n            if(dp[i][j] == 0) continue;\n            int step = min(top[i][j], left[i][j])-1; //\u6B63\u65B9\u5F62\u7684\u5E95\u8FB9\u3001\u53F3\u8FB9\u7684\u6700\u5C0F\u503C\n            for(; step > 0; --step){\n                if(j < step || i < step) continue;\n                if(step <= min(top[i][j-step], left[i-step][j])) break; //\u6B63\u65B9\u5F62\u7684\u5DE6\u8FB9\u3001\u9876\u8FB9\u7684\u6700\u5C0F\u503C\n            }\n            dp[i][j] = dp[i-step][j-step]==0 ? 1 : (step+1) * (step+1); //\u5DE6\u4E0A\u70B9\u4E0D\u80FD\u4E3A0\n        }\n        int res = 0;\n        for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) res = max(res, dp[i][j]);\n        return res;\n    }\n};\n```
[]