Beats 100% || O(1) space || C++ || DP
Intuition\n Describe your first thoughts on how to solve this problem. \xA0\nThe problem requires counting all square submatrices with all 1s in a given binary 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\xA0\nThe problem requires counting all square submatrices with all 1s in a given binary matrix. The key insight is to use dynamic programming to accumulate counts for each square by building on previous results.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tTraverse the matrix, and for each cell (i, j), if it contains a 1, update it to reflect the size of the largest square submatrix ending at that cell. This size can be determined by the minimum size of squares ending at (i-1, j), (i, j-1), and (i-1, j-1), plus 1.\n2.\tAccumulate the values in each cell to get the total count of all square submatrices.\n# Complexity\n- Time complexity: $$O(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& mat) {\n        int m = mat.size(), n = mat[0].size();\n        int cnt = 0;\n        for(int i=0; i<m; i++){\n            for(int j=0; j<n; j++){\n                if(i>0 && j>0 && mat[i][j]){\n                    mat[i][j] = max(mat[i][j], 1+min({mat[i-1][j],mat[i][j-1],mat[i-1][j-1]}));\n                }\n                cnt += mat[i][j];\n            }\n        }\n        return cnt;\n    }\n};\nauto speedup = [](){ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0;}();\n```
['Array', 'Dynamic Programming', 'Matrix', 'C++']