Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Count Square Submatrices with All Ones

XPChallenge Points: 20
levelLevel: Medium

Given an m × n binary matrix matrix (entries are 0 or 1), return the total number of square submatrices that contain only ones.
A square submatrix can have any side length k ≥ 1.

Example 1:

Input: matrix = [ [0,1,1,1], [1,1,1,1], [0,1,1,1] ]

Output: 15

Explanation:

- 1×1 squares: 10 - 2×2 squares: 4 - 3×3 squares: 1 Total = 10 + 4 + 1 = 15

Example 2:

Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ]

Output: 7

Explanation:

- 1×1 squares: 6 - 2×2 squares: 1 Total = 6 + 1 = 7

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result