Java clean solution O(R2 * C) compexity
\nclass Solution {\n    public int numSubmatrixSumTarget(int[][] matrix, int target) {\n        HashMap<Integer, Integer> count = new HashMap();\n        int []
```\nclass Solution {\n    public int numSubmatrixSumTarget(int[][] matrix, int target) {\n        HashMap<Integer, Integer> count = new HashMap();\n        int [] dp = new int [matrix[0].length];\n        int res = 0;\n        for(int i = 0;i <matrix.length;i++){\n            for(int j =i; j < matrix.length;j++){\n                count.put(0, 1);\n                res += numSubmatrixSumTarget(i, j, matrix, target, count, dp);\n                count.clear();\n            }\n        }\n        return res;\n    }\n    \n    private int numSubmatrixSumTarget(int first, int second, int [][] matrix, int target, HashMap<Integer, Integer> count, int [] dp){\n        int res = 0;\n        int sum = 0;\n        for(int i  = 0;i<matrix[first].length;i++){\n            sum += matrix[second][i];\n            dp[i] = sum+ (first == second?0:dp[i]);\n            res += count.getOrDefault(dp[i] - target, 0);\n            count.put(dp[i], count.getOrDefault(dp[i], 0)+1);\n        }\n        return res;\n    }\n}\n```
[]