Skip to main content
LeetCode 750, Medium. Topics: Array, Math, Dynamic Programming, Matrix. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:

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
Example 2
Example 3

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, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026