Java
Presum rects from top-left corner (0, 0) to each btm-right corner and  checking all options at O(n^2 * M^2)Code
Presum rects from top-left corner (0, 0) to each btm-right corner and  checking all options at O(n^2 * M^2)

# Code
```
class Solution {
    public int numSubmatrixSumTarget(int[][] matrix, int target) {
        int m = matrix.length, n = matrix[0].length, ans = 0; 
        int presum[][] = new int [m][n]; // rects sums of (0, 0, i , j) coordinates
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                presum[i][j] = matrix[i][j];
                if (i > 0) presum[i][j] += presum[i - 1][j];
                if (j > 0) presum[i][j] += presum[i][j - 1];
                if (i > 0 && j > 0) presum[i][j] -= presum[i - 1][j - 1]; // since it been added twice
            }
        }

        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                for (int h = -1; h < i; h++){ // upper corner
                    for (int w = -1; w < j; w++){ // left corner
                        int sum = presum[i][j];
                        if (h >= 0) sum -= presum[h][j];
                        if (w >= 0) sum -= presum[i][w];
                        if (h >= 0 && w >= 0) sum += presum[h][w];
                        if (sum == target) ans++;
                    }
                }
            }
        }
        return ans;
    }
}
```
['Java']