Python | DP Solution |
\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        ans = 0\n        for i in range(len(matrix)):\n            for j in rang
```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        ans = 0\n        for i in range(len(matrix)):\n            for j in range(len(matrix[0])):\n                if matrix[i][j]!=0 and i!=0 and j!=0:\n                    matrix[i][j] = min(matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1]) + 1\n                ans += matrix[i][j]\n        return ans\n```\n**I hope that you\'ve found this useful.**\n**In that case, please upvote. It only motivates me to write more such posts\uD83D\uDE03**\nComment below if you have any queries.
['Dynamic Programming', 'Python']