Easy solution biginner friendly | Very easy to understand
Approach\n# \n1. Cumulative Sum for Each Row:\n\n The algorithm starts by computing the cumulative sum for each row in the matrix A. This is done to efficiently
## Approach\n# \n1. Cumulative Sum for Each Row:\n\n* The algorithm starts by computing the cumulative sum for each row in the matrix A. This is done to efficiently calculate the sum of elements in any submatrix.\n# \n2. Iterate Over All Possible Column Pairs:\n\n* The algorithm uses two nested loops to iterate over all possible pairs of columns, denoted by (i, j).\n* For each pair of columns, it calculates the cumulative sum of elements in the submatrix between those columns for each row.\n# \n3. Count Submatrices with Target Sum:\n\n* For each submatrix, the algorithm maintains a defaultdict (c) to count the occurrences of cumulative sums.\n* It uses the variable cur to represent the current sum of elements in the submatrix.\n* The algorithm checks if cur - target has been encountered before. If so, it increments the result (res) by the count of occurrences stored in c[cur - target].\n* The counter c is then updated to reflect the occurrence of the current cumulative sum.\n\n# \n4. Return Result:\n* The final result (res) represents the count of submatrices whose sum meets the target criteria.\n\n## Solution\n```\nclass Solution:\n    def numSubmatrixSumTarget(self, A: List[List[int]], target: int) -> int:\n        # Get the dimensions of the matrix\n        m, n = len(A), len(A[0])\n\n        # Compute the cumulative sum for each row\n        for row in A:\n            for i in range(n - 1):\n                row[i + 1] += row[i]\n\n        res = 0  # Initialize the result variable to count submatrices\n\n        # Iterate over all possible pairs of columns\n        for i in range(n):\n            for j in range(i, n):\n                c = collections.defaultdict(int)\n                cur, c[0] = 0, 1  # Initialize the current sum and a counter for cumulative sums\n\n                # Iterate over each row to calculate the submatrix sum\n                for k in range(m):\n                    # Update the current sum based on the cumulative row sums\n                    cur += A[k][j] - (A[k][i - 1] if i > 0 else 0)\n\n                    # Count submatrices with the target sum using cumulative sums\n                    res += c[cur - target]\n\n                    # Update the counter for the current cumulative sum\n                    c[cur] += 1\n\n        return res\n```
['Python', 'Python3']