DP. Simple solution
\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        n, m = len(matrix), len(matrix[0])\n        ans = sum(matrix[0])\n      
```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        n, m = len(matrix), len(matrix[0])\n        ans = sum(matrix[0])\n        for i in range(1, n):\n            ans += matrix[i][0]\n            for j in range(1, m):\n                if matrix[i][j]:\n                    matrix[i][j] += min(matrix[i-1][j-1], matrix[i][j-1], matrix[i-1][j])\n                    ans += matrix[i][j]\n        return ans     \n```
['Dynamic Programming', 'Python3']