Java Clean Solution
Code\njava []\nclass Solution {\n\n    private int solveSum(int[][] dp, int n, int m) {\n        int sum = 0;\n        for (int i = 0; i < n; i++) {\n          
# Code\n```java []\nclass Solution {\n\n    private int solveSum(int[][] dp, int n, int m) {\n        int sum = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                sum += dp[i][j];\n            }\n        }\n        return sum;\n    }\n\n    public int countSquares(int[][] matrix) {\n        int n = matrix.length, m = matrix[0].length;\n\n        int[][] dp = new int[n][m];\n        for (int j = 0; j < m; j++) {\n            dp[0][j] = matrix[0][j];\n        }\n        for (int i = 0; i < n; i++) {\n            dp[i][0] = matrix[i][0];\n        }\n\n        for (int i = 1; i < n; i++) {\n            for (int j = 1; j < m; j++) {\n                if (matrix[i][j] == 0) {\n                    dp[i][j] = 0;\n                } else {\n                    dp[i][j] = 1 + Math.min(dp[i - 1][j],\n                            Math.min(dp[i - 1][j - 1], dp[i][j - 1]));\n                }\n            }\n        }\n        return solveSum(dp, n, m);\n    }\n}\n```
['Array', 'Dynamic Programming', 'Matrix', 'Java']