Java || Easy || loop through matrix
Complexity\n- Time complexity: O(m * n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(m * n)\n Add your space complexity here, e.g. O(n) 
# Complexity\n- Time complexity: O(m * n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m * n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[][] ver = new int[m][n];\n        int[][] hor = new int[m][n];\n        int max = 0;\n\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 1) {\n                    hor[i][j] = j == 0 ? 1 : hor[i][j - 1] + 1;\n                    ver[i][j] = i == 0 ? 1 : ver[i - 1][j] + 1;\n                }\n            }\n        }\n\n        for (int i = m - 1; i >= 0; i--) {\n            for (int j = n - 1; j >= 0; j--) {\n                int side = Math.min(hor[i][j], ver[i][j]);\n                \n                while (side > max) {\n                    if (ver[i][j - side + 1] >= side && hor[i - side + 1][j] >= side) {\n                        max = side;\n                    }\n                    side--;\n                }\n            }\n        }\n\n        return max * max;\n    }\n}\n```
['Matrix', 'Java']