Count all-ones square submatrices

Write codeMedium20 min

Your task

Given an m x n binary matrix (entries are 0 or 1), count how many square submatrices have all entries equal to 1. Squares of every side length count, and squares overlapping each other are counted separately. Return the total number of all-ones square submatrices. Dimensions are at least 1x1 and at most 300x300.

Examples

count_squares(matrix: list[list[int]]) -> int
Example 1
Inputcount_squares([[0, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1]])
Output15