[Java] Simple DP solution
```\nclass Solution {\n    public int countSquares(int[][] matrix) {\n        \n        int n = matrix.length;\n        int m = matrix[0].length;\n        int[]
```\nclass Solution {\n    public int countSquares(int[][] matrix) {\n        \n        int n = matrix.length;\n        int m = matrix[0].length;\n        int[][] dp = new int[n][m];\n        int count = 0;\n        \n        for(int i = 0; i < n; i++) if(matrix[i][0] == 1) dp[i][0] = 1;\n        for(int j = 0; j < m; j++) if(matrix[0][j] == 1) dp[0][j] = 1;\n\t\t\n        // dp[i][j] will tell you the max side of the square with bottom right corner at (i, j).\n        for(int i = 1; i < n; i++){\n            for(int j = 1; j < m; j++){\n                if(matrix[i][j] == 1){\n                    int min = 1 + Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1]));\n                    dp[i][j] = min;\n                }\n            }\n        }\n        \n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < m; j++) count += dp[i][j];\n        }\n        \n        return count;\n        \n    }\n}
['Dynamic Programming']