


Ninja has been given a matrix ‘MAT’ of integers having size ‘N’ x ‘M’ i.e. N rows and M columns. Ninja has to find the maximum sum submatrix in it. In other words, he has to find the maximum sum over all submatrices in the matrix.
For example: For the ‘MAT’ given below, the maximum sum submatrix having a sum of 29 is highlighted in red.

The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ representing the number of rows and columns respectively. size of the matrix ‘MAT’.
The next ‘N’ lines of each test case contain ‘M’ single space-separated integers denoting the values of ‘MAT’.
Output Format :
For each test case, print the maximum sum over all submatrices in ‘MAT’.
Print the output of each test case in a separate line.
Note :
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= ‘T’ <= 100
1 <= ‘N’, ‘M’<= 100
-100000 <= ‘MAT[i][j]’ <=100000
Where 'MAT[i][j]' represents the value at cell(i, j).
Time limit: 1 sec
1
4 4
-1 -2 -2 -2
-5 -4 -1 -9
-3 -2 -6 -3
-4 -3 -3 -2
-1
For sample test case 1 :

In this sample test case, the maximum sum submatrix in the matrix ‘MAT’ is at cell (0,0) with sum -1.
1
3 3
1 2 3
4 5 6
7 8 9
45
For sample test case 1 :

In this sample test case, the maximum sum submatrix in the matrix ‘MAT’ is having cells (0,0), (0,2), (2,0), (2,2) with sum 45.
Think of the Brute Force Approach.
The brute force approach is we check for every possible submatrix in the given ‘MAT’.
Here is the algorithm :
O((N * M) ^ 3) Where ‘N’ and ‘M’ denote the number of rows and columns of the matrix ‘MAT’.
Because we are using 6 nested loops. 4 nested loops are required to select every submatrix of the matrix And 2 nested loops are required to take the sum of all the elements of the submatrix. Hence, the overall time complexity is O((N * M) ^ 3).
O(1).
Because we are not using any extra space for finding our resultant answer.