


You are given ‘MAT’= [[1, 2],[3, 4]] and ‘K’ = 8.

Then the answer will be 7 (the sum of the red rectangle).
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains three space-separated integers, ‘N’ and ‘M’ representing the number of rows and columns in ‘MAT’, respectively, and ‘K’.
The next ‘N’ lines contain ‘M’ space-separated integers representing the elements of ‘MAT’.
For each test case, print the maximum sum of a rectangle in the matrix which is less than or equal to ‘K’
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
-10^6 <= K <= 10^6
-10^6 <= MAT[i][j] <= 10^6
Time Limit: 1 sec
Prerequisite: Subarray sum equals K
In this approach, we will first calculate the prefix sum of each row. We will use the input matrix itself to store the prefix sum of each row. We will get the range sum for every two columns using the subtraction of the prefix sum. Then for every index ‘X’, we will store the prefix sums in a sorted container. Now, to find the maximum possible sum which is less than or equal to ‘K’, we will find if there is a possible sub-array with sum ‘K’ that ends at ‘X’ and if not, will try to find the sum with possible sum k -1 and so on.
Algorithm :
Prerequisite: Kadane’s algorithm
Kadane’s algorithm is used to find a maximum sum contiguous subarray within a one-dimensional numeric array in O(N) time. In this approach, we will create an integer array that will store the prefix sums of each row from column i to j where 0 < ‘I’ < ‘J’ < ‘M’. We will run Kadane’s algorithm on each such array to get the maximum possible sum closest to ‘K’ of any subarray of it in the same way as we have done in approach 1.
Algorithm :