You are given a
matrix , filled with distinct numbers. You can start at any cell. If you are currently at cell , then you can jump to any other cell , if and only if the following two conditions are satisfied:
Manhattan distance between and . Formally, .
Let be the set of the cells visited by you.
Maximize the value of .
Input Format
- The first line of the input contains the number - the number of test cases. Then the test cases follow.
- The first line of each test case contains three integers - the parameters defined in the statement.
- Then lines follow, with integers in each line.
- Each of the next lines contains integers, where the -th integer in -th line is equal to
Output Format
For each testcase output the maximum possible value of .
Constraints
- .
- .
- It's guaranteed that all are distinct.
- It's guaranteed that sum of over all test cases doesn't exceed .
Sample Input 1
2
3 2 2
1 2
4 5
7 8
2 2 0
100 99
98 97
Sample Output 1
9
394
Explanation
Test case-1: It is optimal start at cell , then jump to cell . Therefore answer is .
Test case-2: It is optimal to start at and follow this path: . Therefore answer is the sum of all the four cells .
ConversionConversion EmoticonEmoticon