Python DP O(n*m) solution in 7 lines
\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        res = 0\n        for i, row in enumerate(matrix):\n            for j, n in enumerate(row)
\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        res = 0\n        for i, row in enumerate(matrix):\n            for j, n in enumerate(row):\n                if i != 0 and j != 0 and matrix[i][j] == 1:\n                    matrix[i][j] += min(matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1])\n                res += matrix[i][j]\n        return res
['Dynamic Programming', 'Python3']