Number of Corner Rectangles

Medium

Description

Given a grid of 0s and 1s, count the number of axis-aligned rectangles that can be formed using only grid cells marked with 1 as corners.

Examples

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:

One corner rectangle.

Input:grid = [[1]]
Output:1
Explanation:

Minimal case with a single-element matrix.

Input:grid = [[1,1,0],[1,1,0],[0,0,1]]
Output:1
Explanation:

There is one corner rectangle formed by the 1s at positions (0,0), (0,1), (1,0), and (1,1). These four 1s form a 2x2 rectangle in the top-left corner of the grid.

Constraints

  • 1 ≤ rows, cols ≤ 200

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!