3ms 95% Faster Solution
java code is:\n# \n\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int n=grid.length,m=grid[0].length;\n        int left[][]=
java code is:\n# \n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int n=grid.length,m=grid[0].length;\n        int left[][]=new int[n][m];\n         int down[][]=new int[n][m];\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                left[i][j]=grid[i][j]==0 ? 0 : 1+(j-1>=0 ? left[i][j-1] : 0);\n                down[i][j]=grid[i][j]==0 ? 0 : 1+(i-1>=0 ? down[i-1][j] : 0);\n            }\n        }\n        int res=0;\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n               int min=Math.min(down[i][j],left[i][j]);\n               while(min>res){\n                   if(left[i-min+1][j]>=min && down[i][j-min+1]>=min){\n                       res=min;\n                       break;\n                   }\n                   min--;\n               } \n            }\n        }\n        return res*res;\n    }\n}\n```\n***Please,Upvote if this is helpful***
[]