#P16636. [GKS 2017 #G] Matrix Cutting

[GKS 2017 #G] Matrix Cutting

Problem Description

Prof Shekhu has a matrix of NN rows and MM columns where rows are numbered from 00 to N1N-1 from top to bottom, and columns are numbered from 00 to M1M-1 from left to right. Each cell in the matrix contains a positive integer.

He wants to cut this matrix into N×MN \times M submatrices (each of size 1×11 \times 1) by making horizontal and vertical cuts. A cut can be made only on the boundary between two rows or two columns.

Prof Shekhu invites his best student Akki for this job and makes an interesting proposition. Every time Akki makes a cut in a submatrix, before he makes the cut, he is awarded a number of coins equal to the minimum value in that submatrix. Note that with every cut, the total number of submatrices increases. Also, cuts in any two different submatrices are independent and likewise, Akki is awarded independently for the cuts in different submatrices.

Now, Akki has various ways in which he can make the cuts. Can you help him by maximizing the total number of coins he can gain?

Input Format

The first line of the input contains an integer TT, the number of test cases. TT test cases follow.

The first line of each test case contains two integers NN and MM, as described above.

  1. Next, there are NN lines of MM positive integers each; these describe the matrix.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the maximum possible number of coins that Akki can be awarded, if he makes the cuts in optimal order.

3
2 2
1 2
3 4
2 3
1 2 1
2 3 2
1 2
1 2
Case #1: 5
Case #2: 7
Case #3: 1

Hint

In Sample Case 11, there are two possible ways in which Akki can make the cuts.

  1. Suppose that Akki first cuts the matrix horizontally. He is awarded the minimum value in the matrix: 11. Then he has to make vertical cuts in the two submatrices ([1,2][1, 2] and [3,4][3, 4]), for which he gets 11 and 33 coins, respectively.
  2. Suppose that Akki first cuts the matrix vertically. He is awarded the minimum value in the matrix: 11. Then he has to make horizontal cuts in the two submatrices (which have the transposes [1,3][1, 3] and [2,4][2, 4]), for which he gets 11 and 22 coins, respectively.

The first strategy is better, and the answer is 55.

In Sample Case 22, Akki can be awarded at most 77 coins. One of the optimal ways is to first make the only horizontal cut to earn 11 coin. Then, in the upper submatrix [1,2,1][1, 2, 1], Akki can first make the cut immediately to the right of first column and then the cut immediately to the right of second column to earn a total of 22 coins. Similarly, in the lower submatrix [2,3,2][2, 3, 2], Akki can first make the cut immediately to the right of second column and then the cut immediately to the right of first column to earn a total of 44 coins.

In Sample Case 33, there is only one cut to be made.

Limits

1T1001 \le T \le 100.

11 \le each value in the matrix 105\le 10^5.

Small dataset (Test set 1 - Visible)

N=1N = 1.

1M101 \le M \le 10.

Large dataset (Test set 2 - Hidden)

1N401 \le N \le 40.

1M401 \le M \le 40.