> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number Of Corner Rectangles Python Solution

> Tested Python solution for LeetCode 750 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 750, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/number-of-corner-rectangles/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 750   # by problem number
lcpy gen -s number_of_corner_rectangles   # by problem name
```

## Problem

Given an `m x n` integer matrix `grid` where each entry is only `0` or `1`, return the number of **corner rectangles**.

A **corner rectangle** is four distinct `1`'s on the grid that form an axis-aligned rectangle. Note that only the corners need to have the value `1`. Also, all four `1`'s used must be distinct.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0750.Number%20Of%20Corner%20Rectangles/images/cornerrec1-grid.jpg)

```
Input: grid = [[1,0,0,1,0],[0,0,1,0,1],[0,0,0,1,0],[1,0,1,0,1]]
Output: 1
Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0750.Number%20Of%20Corner%20Rectangles/images/cornerrec2-grid.jpg)

```
Input: grid = [[1,1,1],[1,1,1],[1,1,1]]
Output: 9
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
```

![Example 3](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0750.Number%20Of%20Corner%20Rectangles/images/cornerrec3-grid.jpg)

```
Input: grid = [[1,1,1,1]]
Output: 0
Explanation: Rectangles must have four distinct corners.
```

### Constraints

* m == grid.length
* n == grid\[i].length
* 1 \<= m, n \<= 200
* grid\[i]\[j] is either 0 or 1.
* The number of 1's in the grid is in the range \[1, 6000].

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_corner_rectangles/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_corner_rectangles/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(m * n^2)
    # Space: O(n^2)
    def count_corner_rectangles(self, grid: list[list[int]]) -> int:
        ans = 0
        cnt: Counter[tuple[int, int]] = Counter()
        for row in grid:
            ones = [i for i, v in enumerate(row) if v]
            for a in range(len(ones)):
                for b in range(a + 1, len(ones)):
                    pair = (ones[a], ones[b])
                    ans += cnt[pair]
                    cnt[pair] += 1
        return ans
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n^2) | O(n^2) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.