#K3216. Maximum Sum Submatrix
Maximum Sum Submatrix
Maximum Sum Submatrix
You are given a two-dimensional matrix of integers. Your task is to find the maximum sum of any non-empty contiguous submatrix. A submatrix is defined as a contiguous rectangular block of elements in the matrix. In mathematical terms, if the submatrix covers rows from r1 to r2 and columns from c1 to c2, then its sum is:
\[ S = \sum_{i=r_1}^{r_2} \sum_{j=c_1}^{c_2} a_{ij} \]
Your solution should efficiently handle matrices that might include negative numbers.
inputFormat
The first line of the input contains an integer T
representing the number of test cases. Each test case begins with a line containing two integers N
and M
(the number of rows and columns, respectively). This is followed by N
lines, each containing M
space-separated integers representing the matrix.
For example:
1 4 5 1 2 -1 -4 -20 -8 -3 4 2 1 3 8 10 1 3 -4 -1 1 7 -6
outputFormat
For every test case, output a single line containing the maximum sum of any submatrix in the given matrix.
## sample1
4 5
1 2 -1 -4 -20
-8 -3 4 2 1
3 8 10 1 3
-4 -1 1 7 -6
29
</p>