#P16730. [GKS 2019 #C] Circuit Board

[GKS 2019 #C] Circuit Board

Problem Description

Arsh recently found an old rectangular circuit board that he would like to recycle. The circuit board has RR rows and CC columns of squares.

Each square of the circuit board has a thickness, measured in millimetres. The square in the rr-th row and cc-th column has thickness Vr,cV_{r,c}. A circuit board is good if in each row, the difference between the thickest square and the least thick square is no greater than KK.

Since the original circuit board might not be good, Arsh would like to find a good subcircuit board. A subcircuit board can be obtained by choosing an axis-aligned subrectangle from the original board and taking the squares in that subrectangle. Arsh would like your help in finding the number of squares in the largest good subrectangle of his original board.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case begins with one line containing three integers RR, CC and KK, the number of rows, the number of columns, and the maximum difference in thickness allowed in each row.

Then, there are RR more lines containing CC integers each. The cc-th integer on the rr-th line is Vr,cV_{r,\, c}, the thickness of the square in the rr-th row and cc-th column.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of squares in a good subrectangle.

3
1 4 0
3 1 3 3
2 3 0
4 4 5
7 6 6
4 5 0
2 2 4 4 20
8 3 3 3 12
6 6 3 3 3
1 6 8 6 4
Case #1: 2
Case #2: 2
Case #3: 6
3
1 4 2
3 1 3 3
3 3 2
0 5 0
8 12 3
7 10 1
4 4 8
20 10 20 10
10 4 5 20
20 5 4 10
10 20 10 20
Case #1: 4
Case #2: 3
Case #3: 4

Hint

Additional Sample - Test Set 2

The sample cases are illustrated below. For each case, the good subcircuit board with the largest number of squares is highlighted in green.

:::align{center} :::

Limits

1T501 \le T \le 50.

1R3001 \le R \le 300.

1C3001 \le C \le 300.

0Vi,j1030 \le V_{i,\,j} \le 10^3 for all ii, jj.

Test set 1 (Visible)

K=0K = 0.

Test set 2 (Hidden)

0K1030 \le K \le 10^3.