Using dynamic Programming.
Intuition\n Describe your first thoughts on how to solve this problem. \nBy creating a matrix of same size as of the given matrix and fill it cells by seeing of
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy creating a matrix of same size as of the given matrix and fill it cells by seeing of which squares it can be the rightmost bottom.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe first row and column of dp matrix will be same as that of given matrix and for other cells its value will only depend on the adjacent above of it as it will become their rightmost bottom min of these and plus 1 as it also count as a single square. then iterate the dp matrix and add all the values present to calculate total sqaures.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N*M)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N*M)\n\n# Code\n```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        n = len(matrix)\n        m = len(matrix[0])\n        dp = [[0 for _ in range(m)]for _ in range(n)] # Creating a dp matrix of the same size that of matrix\n        for j in range(m):\n            dp[0][j] = matrix[0][j]\n        for i in range(n):\n            dp[i][0] = matrix[i][0] # Copying the first row and first column as there value does not change\n        for i in range(1,n):\n            for j in range(1,m): # Iterating the matrix from 1 as 0 index is already done \n                if matrix[i][j] == 0:\n                    dp[i][j] = 0 # If 0 is present then it will not help in making any square\n                else:\n                    # To fill a cell in dp matrix we just need to know the adjacent cells of that particular matrix \n                    # only the 3 ajdacent cell of which it will be the right bottom\n                    dp[i][j] = 1+ min(dp[i-1][j],min(dp[i-1][j-1],dp[i][j-1]))\n        # Iterating the dp matrix and adding the values to know number of squares\n        result = 0\n        for i in range(n):\n            for j in range(m):\n                result += dp[i][j]\n        return result\n```
['Python3']