[Java/C++/Python] DP solution
Explanation\ndp[i][j] means the size of biggest square with A[i][j] as bottom-right corner.\ndp[i][j] also means the number of squares with A[i][j] as bottom-ri
## **Explanation**\n`dp[i][j]` means the size of biggest square with `A[i][j]` as bottom-right corner.\n`dp[i][j]` also means the number of squares with `A[i][j]` as bottom-right corner.\n\nIf `A[i][j] == 0`, no possible square.\nIf `A[i][j] == 1`,\nwe compare the size of square `dp[i-1][j-1]`, `dp[i-1][j]` and `dp[i][j-1]`.\n`min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1` is the maximum size of square that we can find.\n<br>\n\n## **Complexity**\nTime `O(MN)`\nSpace `O(1)`\n<br>\n\n**Java:**\n```java\n    public int countSquares(int[][] A) {\n        int res = 0;\n        for (int i = 0; i < A.length; ++i) {\n            for (int j = 0; j < A[0].length; ++j) {\n                if (A[i][j] > 0 && i > 0 && j > 0) {\n                    A[i][j] = Math.min(A[i - 1][j - 1], Math.min(A[i - 1][j], A[i][j - 1])) + 1;\n                }\n                res += A[i][j];\n            }\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    int countSquares(vector<vector<int>>& A) {\n        int res = 0;\n        for (int i = 0; i < A.size(); ++i)\n            for (int j = 0; j < A[0].size(); res += A[i][j++])\n                if (A[i][j] && i && j)\n                    A[i][j] += min({A[i - 1][j - 1], A[i - 1][j], A[i][j - 1]});\n        return res;\n    }\n```\n\n**Python:**\n```python\n    def countSquares(self, A):\n        for i in xrange(1, len(A)):\n            for j in xrange(1, len(A[0])):\n                A[i][j] *= min(A[i - 1][j], A[i][j - 1], A[i - 1][j - 1]) + 1\n        return sum(map(sum, A))\n```\n
[]