Count Submatrices With All Ones

Given an m x n binary matrix mat, return the number of submatrices that have all ones.

Example 1
1 0 1
1 1 0
1 1 0
Inputmat = [[1,0,1],[1,1,0],[1,1,0]]
Output13
The all-one rectangles include 6 of size 1x1, 2 of size 1x2, 3 of size 2x1, 1 of size 2x2, and 1 of size 3x1, for a total of 13.
Example 2
0 1 1 0
0 1 1 1
1 1 1 0
Inputmat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
Output24
The all-one rectangles include 8 of size 1x1, 5 of size 1x2, 2 of size 1x3, 4 of size 2x1, 2 of size 2x2, 2 of size 3x1, and 1 of size 3x2, for a total of 24.

Constraints

  • 1 <= m, n <= 150
  • mat[i][j] is either 0 or 1.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate