Python 🐍 Thought process for the  DP solution with very simple explanation (with images)
The easiest way to crack this problem is to ask how can I use the previous value to my advantage ? \n\n\n\n\nNote 1: So now we know how to check if a matrix wil
The easiest way to crack this problem is to ask how can I use the previous value to my advantage ? \n\n![image](https://assets.leetcode.com/users/images/48e1e213-9c13-4389-9ab1-c67b7cacd88e_1643709690.1342795.png)\n\n\n**Note 1:** So now we know how to check if a matrix will form a square or not, but as you seen in the image above the cell (1,1) that we  were checking is originally \'1\', so it was worth checking, what if it was \'0\', then no need to do the min because it will never form a square, so we just keep it \'0\' as is. \n\n\n\nThis is the solution for Lettcode #221 Maximal square, however this problem is asking for the \'**Count\'**, so I need to count how many squares.\n\n**Note 2:** What will be the case for the top row (row=0) and (col=0), well the top row and top column will never form a square, so in my for loop i will check if row==0 or col==0, I will continue --> this is correct, however this problem told us to consider \'1\' as a square of side=1 so we need to count it --> well, we can fix , I will  always increment my count variable if matrix[r][c]==1 , then do the check if row 0 or col 0, continue.\n\n![image](https://assets.leetcode.com/users/images/80e293d5-b34f-4357-9ede-b38249624694_1643710710.830303.png)\n\n\n```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        \n        count=matrix.count(1)\n        count=0\n        for r in range(len(matrix)):\n            for c in range(len(matrix[0])):\n                if matrix[r][c]==1:\n                    count+=1\n                if r==0 or c==0: continue\n                \n                old_val=matrix[r][c]\n                matrix[r][c]=min(matrix[r-1][c-1], matrix[r][c-1],matrix[r-1][c]) + 1 if matrix[r][c]==1 else 0\n                count= count+ matrix[r][c]- old_val  \n        return count\n```
['Dynamic Programming', 'Python', 'Python3']