Python 3 | DP solution with comments
\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        \n        m = len(matrix)\n        n = len(matrix[0])\n        \n       
```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        \n        m = len(matrix)\n        n = len(matrix[0])\n        \n        dp = [[0 for _ in range(n)] for _ in range(m)]\n        \n        total = 0\n        \n        # initialize the dp matrix for the row\n        for i in range(m):\n            # each time the entry is equal to 1, increment total\n            if matrix[i][0] == 1:\n                dp[i][0] = 1\n                total += 1\n                \n        # initialize the dp matrix fro the column\n        for i in range(1,n):\n            # each time the entry is euqal to 1, increment total\n            if matrix[0][i] == 1:\n                dp[0][i] = 1\n                total += 1\n        \n        # for the rest of the entries, the dp entry is the min of the upper-left, left, upper entries plus 1\n        for i in range(1, m):\n            for j in range(1, n):\n                if matrix[i][j] != 0:\n                    \n                    # in order to make a square, we have to look at the upper-left, left, upper entries for the cell\n                    dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1\n                    \n                    # add the total\n                    total += dp[i][j]\n        print(dp)\n        return total\n                \n        \n```
[]