Simple Python Solution O(mn) Time and O(1) Space
This problem is an easier version of this problem, and the logic can be inferred from this:\nhttps://leetcode.com/problems/maximal-rectangle/\n\n\n\n\nclass Sol
This problem is an easier version of this problem, and the logic can be inferred from this:\nhttps://leetcode.com/problems/maximal-rectangle/\n\n\n\n```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        count=0 #results counter\n        for r in range(len(matrix)):\n            for c in range(len(matrix[0])):\n                if r>0 and c>0 and matrix[r][c]: \n\t\t\t\t#For each cell, I modify the value to the largest square of 1\'s that can be created with the cell as the bottom right corner of the square.  \n                    matrix[r][c]=min(matrix[r-1][c-1],matrix[r-1][c],matrix[r][c-1])+1 #DP\n\n                count+=matrix[r][c]\n        \n        return count\n```\n\n
[]