C++ Solution with explanation || White Board Explanation
\n\n\nCredit to @arkaung for such a nice explanation\n\nExplanation\n\ndp[i][j] means the size of biggest square with A[i][j] as bottom-right corner.\ndp[i][j] 
![image](https://assets.leetcode.com/users/images/75603b82-0ea7-45af-914e-0f8130fec1af_1615174401.6347303.png)\n![image](https://assets.leetcode.com/users/images/aef8723a-eb59-4bac-9ca5-0f48174a612a_1615174410.8267586.png)\n![image](https://assets.leetcode.com/users/images/6b785719-ecaf-4b01-98ed-e756546920ef_1615174426.9130447.png)\nCredit to @arkaung for such a nice explanation\n\n**Explanation**\n\ndp[i][j] means the size of biggest square with A[i][j] as bottom-right corner.\ndp[i][j] also means the number of squares with A[i][j] as bottom-right corner.\n\nIf A[i][j] == 0, no possible square.\nIf A[i][j] == 1,\nwe compare the size of square dp[i-1][j-1], dp[i-1][j] and dp[i][j-1].\nmin(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1 is the maximum size of square that we can find.\n\n\n**Complexity**\n\nTime O(MN)\nSpace O(1)\n```\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& matrix) {\n        \n        int n=matrix.size(); int m=matrix[0].size();\n        int cell_val; int result=0;\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(matrix[i][j]==1){\n                    if(i==0 || j==0)\n                        result+=1;\n                    else{\n                       cell_val=min(matrix[i-1][j],min(matrix[i][j-1],matrix[i-1][j-1]))+matrix[i][j];\n                        result+=cell_val;\n                        matrix[i][j]=cell_val;\n                    }       \n                }\n            }\n        }\n        return result;\n    }\n}\n```
[]