Python 6 lines of code solution with explanation
The main idea to calculate how many time each cell could be part of big square(s).\nSo  the matrix:\n\n[\n  [0,1,1,1],\n  [1,1,1,1],\n  [0,1,1,1]\n]\n\nCould be
The main idea to calculate how many time each cell could be part of big square(s).\nSo  the matrix:\n```\n[\n  [0,1,1,1],\n  [1,1,1,1],\n  [0,1,1,1]\n]\n```\nCould be tranform into:\n```\n[\n  [0,1,1,1],\n  [1,1,2,2],\n  [0,1,2,3]\n]\n```\nWhere  cells  ``[2,1] [3,1]`` and  ``[2,2]`` included twice and ``[3,2]`` three times: \nSo after that we can easy calculate total number of squares:\n\n```\nclass Solution:\n    def countSquares(self, matrix: List[List[int]]) -> int:\n        total =0\n        for i in range(len(matrix)):\n            for j in range(len(matrix[0])):\n                if i>0 and j>0 and matrix[i][j]==1:\n                    matrix[i][j] = matrix[i][j]+ min(matrix[i][j-1],matrix[i-1][j-1],matrix[i-1][j])\n                total +=matrix[i][j]\n        return total \n```\n\n
[]