C++ DP KISS explained (97%/100%)
We\'re replacing each element with the max square it can make at that corner (lower-right)\nthat is just the min of the max squares of its upper-left neighbors\
We\'re replacing each element with the max square it can make at that corner (lower-right)\nthat is just the min of the max squares of its upper-left neighbors\njust add up the numbers to get the result\n```\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& M) {\n        int r=0;\n        for(int i=0;i<M.size();++i) //all rows\n            for(int j=0;j<M[i].size();++j){ // all columns\n                if(i and j and M[i][j]) // exclude first row/col and zeros\n                    M[i][j] = min(M[i-1][j-1],min(M[i-1][j],M[i][j-1]))+1; // find max size of square at this corner by looking one step backwards\n                r+=M[i][j]; // add number of squares (max size) at this corner\n            }\n        return r;\n    }\n};\n```
[]