#K50347. Maximum Sum Submatrix

    ID: 28844 Type: Default 1000ms 256MiB

Maximum Sum Submatrix

Maximum Sum Submatrix

You are given an integer matrix with N rows and M columns. Your task is to find the maximum sum of all elements in any submatrix of size r × c.

Each test case begins with a line containing three integers: N, M, and an extra integer K (which can be ignored). The next line contains two integers r and c — the number of rows and columns of the submatrix.

This problem can be modeled using the following formula for a given submatrix starting at row i and column j: $$ S(i,j)=\sum_{x=0}^{r-1}\sum_{y=0}^{c-1} A_{i+x,j+y} $$ Your goal is to compute the maximum possible value of \(S(i,j)\) over all valid top-left positions of the submatrix in the matrix.

The input is read from standard input and the output should be printed to standard output.

inputFormat

The first line of the input contains a single integer T representing the number of test cases. For each test case, the input is given as follows:

  • A line with three integers N, M, and K (the third integer is not used in computations).
  • A line with two integers r and c.
  • N subsequent lines each containing M integers, representing the rows of the matrix.

All input is provided via standard input.

outputFormat

For each test case, output a single line containing the maximum submatrix sum. The answers for all test cases should be printed in the same order as the test cases appear in the input.

All output should be sent to standard output.

## sample
3
4 5 1
2 3
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
2 2 1
1 1
10 -10
20 30
5 5 1
3 3
0 -2 -7 0 -1
9 2 -6 2 0
-4 1 -4 1 0
-1 8 0 -2 1
-10 1 1 -5 7
99

30 5

</p>