Python DP solution with detail explaination
def countSquares(self, matrix: List[List[int]]) -> int:\n\t\t\t\n\t\t#Define square to save the maximum size of the Square formed by the number present at each 
def countSquares(self, matrix: List[List[int]]) -> int:\n\t\t\t\n\t\t#Define square to save the maximum size of the Square formed by the number present at each index\n\t\t#square will have the same dimensions as that of matrix\n\t\tsquare = []\n        \n        #Iterate over each row of the matrix\n        for i in range(len(matrix)):\n            \n            #Add new row to the square\n            square.append([])\n            \n            #Iterate over each element of the matrix\n            for j in range(len(matrix[0])):\n                #Size of the maximum square formed by the elements from First row and column will be equal to teh number itself\n                if i==0 or j==0: square[i].append(matrix[i][j])\n                    \n                #If number=1 then the size of max square will be 1+minimum of previous adjacent numbers\n                elif matrix[i][j]: square[i].append(1 + min(square[i-1][j], square[i][j-1], square[i-1][j-1]))\n                #If number=0 then the max size of the square will be 0 \n                else: square[i].append(0)\n                    \n        #count the total sqaures formed by taking the sum of all the sqaure formed            \n        count = 0\n        for sq in square: count += sum(sq)\n        return count\n
['Dynamic Programming']