Easy DP Solution | Python | C++
Code\npython []\nclass Solution(object):\n    def countSquares(self, matrix):\n\n        m,n = len(matrix[0]), len(matrix)\n        ans = 0\n        for i in ra
# Code\n```python []\nclass Solution(object):\n    def countSquares(self, matrix):\n\n        m,n = len(matrix[0]), len(matrix)\n        ans = 0\n        for i in range(n-1,-1,-1):\n            for j in range(m-1, -1, -1):\n                if i+1 < n and j+1 < m and matrix[i][j] == 1:\n                    matrix[i][j] = min(matrix[i+1][j], matrix[i][j+1], matrix[i+1][j+1]) + 1\n            ans += sum(matrix[i])\n        return ans\n```\n```C++ []\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& matrix) {\n\n        int m = matrix.size();\n        int n = matrix[0].size();\n        int ans = 0;\n\n        for (int i = m - 1; i >= 0; i--) {\n            for (int j = n - 1; j >= 0; j--) {\n                if (matrix[i][j] == 1) {\n                    if (i + 1 < m && j + 1 < n) {\n                        matrix[i][j] += std::min({matrix[i + 1][j], matrix[i][j + 1], matrix[i + 1][j + 1]});\n                    }\n                    ans += matrix[i][j];\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/150b80d0-0a72-4f75-93a2-e1a93b595564_1720902389.7207024.jpeg)\n\n
['Array', 'Dynamic Programming', 'Matrix', 'Python', 'C++']