[Java] O(n^3) : With Detailed Explanation (step by step)
The idea is to fix the left and right columns one by one and count sub-arrays for every left and right column pair. Calculate sum of elements in every row from 
* The idea is to fix the left and right columns one by one and count sub-arrays for every left and right column pair. Calculate sum of elements in every row from left to right and store these sums in an array say temp[]. So temp[i] indicates sum of elements from left to right in row i.\n\n* Count sub-arrays in temp[] having sum equal to  target. This count is the number of sub-matrices having sum equal to  target with left and right as boundary columns. Sum up all the counts for each temp[] with different left and right column pairs.\n\n```\nclass Solution {\n    \n    public int numSubmatrixSumTarget(int[][] matrix, int target) {\n        // Number of columns\n        int n=matrix[0].length;\n        // Number of rows\n        int m=matrix.length;\n        // store the total count of such sub matrices in result variable\n        int result=0;\n        \n        // Fix the left boundary\n        for(int left=0;left<n;left++){\n            // Fix the right boundary\n            int[] temp=new int[m];\n            for(int right=left;right<n;right++){\n                // Compute sum of row elements between the left and right boundary\n                for(int k=0;k<m;k++){\n                    temp[k]+=matrix[k][right];\n                }\n                // Check for target sum\n                result+=subArraySumToTarget(temp,target);\n            }\n        }\n        return result;\n    }\n    \n    // Works in O(n)\n    int subArraySumToTarget(int[] temp,int target){\n        // Use a hashmap of running sum and frequency of that sum\n        HashMap<Integer,Integer> map=new HashMap<>();\n        int runningSum=0;\n        int count=0;\n        map.put(0,1);\n        for(int i=0;i<temp.length;i++){\n            runningSum+=temp[i];\n            if(map.containsKey(runningSum-target)){\n                count+=map.get(runningSum-target);\n            }\n            map.put(runningSum,map.getOrDefault(runningSum,0)+1);\n        }\n        return count;\n    }\n    \n}\n```
[]