Max sum submatrix
Easy Accuracy: 67.89% Submissions: 333 Points: 2

Given a matrix of size NxM and a list of queries containing (a,b) pairs. Find the maximum sum among all (a x b) sub-matrices of the matrix.
Note: The rows and columns of the submatrix must be contiguous. For example:

Example 1:

Input:
N = 3, M = 4
mat[][] = {{1, 2, 3, 9},
{4, 5, 6, 2},
{8, 3, 2, 6}}
Q = 1
Queries[] = {(3,2)}

Output: 28

Explanation:
Here a = 3 and b = 2.

The first 3x2 submatrix is:
1 2
4 5
8 3
The sum of elements in this is 23.

The second 3x2 submatrix is:
2 3
5 6
3 2
The sum of elements in this is 21.

The third 3x2 submatrix is:
3 9
6 2
2 6
The sum of elements in this is 28.

The maximum among these is 28.

Example 2:

Input:
N = 3, M = 4
mat[][] = {{1, 2, 3, 9},
{4, 5, 6, 2},
{8, 3, 2, 6}}
Q = 3
Queries[] = {(1, 1), (2, 2), (3, 3)}

Output: 9 20 38

Example 3:

Input:
N = 2, M = 7
mat[][] = {{49, 44, 46, 22, 24, 11, 2},
{5, 25, 24, 28, 20, 47, 47}}
Q = 1
Queries = {(2, 1)}

Output: 70

You don't need to read input or print anything. Your task is to complete the function maxSubMatrixSumQueries() which takes the 2D array of integers mat, n, m, queries and q as input parameters and returns an array of integers denoting the maximum sum for each query.

Expected Time Complexity: O(Q*N*M)
Expected Auxiliary Space: O(N*M)

Constraints:
1 ≤ N ≤ 103
1 ≤ M ≤ 103
1 ≤ Q ≤ 100
1 ≤ a ≤ N
1 ≤ b ≤ M
1 ≤ mat[i][j] ≤ 103

We are replacing the old Disqus forum with the new Discussions section given below.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

#### My Submissions:  