dp[i][j] = 1+ min(dp[i][j-1],dp[i-1][j-1],dp[i-1][j])
\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& mat) {\n        int n = mat.size();\n        int m = mat[0].size();\n        int res = 0;
```\nclass Solution {\npublic:\n    int countSquares(vector<vector<int>>& mat) {\n        int n = mat.size();\n        int m = mat[0].size();\n        int res = 0;\n        vector<vector<int>> dp(n,vector<int>(m,0));\n        \n        for(int i=0;i<n;i++)\n        {\n            if(mat[i][0] == 1){\n                dp[i][0] = 1;\n                res++;\n            }\n        }\n        \n        for(int i=1;i<m;i++)\n        {\n            if(mat[0][i] == 1){\n                dp[0][i] = 1;\n                res++;\n            }\n        }\n        \n        \n        \n        for(int i=1;i<n;i++)\n        {\n            for(int j=1;j<m;j++)\n            {\n                if(mat[i][j] == 1){\n                    dp[i][j] = 1+ min({dp[i][j-1],dp[i-1][j-1],dp[i-1][j]});\n                }\n                    res+=dp[i][j];                \n                \n            }\n        }\n        \n        return res;\n    }\n};\n```
[]