This page is still under construction.

Parts of this page are still being built. What you see may change.

Circuit Board

Time limit15sMemory limit1024 MB

Summary
Find the largest axis-aligned subrectangle where each row's max minus min is at most K, and report its number of squares.
Level

Medium6 of 10

Topics
Sliding window, Two pointers, Array
Solved
No attempts yet

Problem

Arsh recently found an old rectangular circuit board that he would like to recycle. The board has RR rows and CC columns of squares. Each square has a thickness in millimetres. The square in row rr and column cc has thickness Vr,cV_{r,c}.

A circuit board is good if in each row, the difference between the thickest square and the thinnest square is at most KK. The original board might not be good, so Arsh wants to find a good subcircuit board. A subcircuit board is an axis-aligned subrectangle of the original board.

Find the number of squares in the largest good subrectangle.

Input

The first line contains TT, the number of test cases. Each test case begins with a line containing RR, CC, and KK. Then RR lines follow, each with CC integers. The cc-th integer on the rr-th line is Vr,cV_{r,c}.

Output

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

Constraints

1≤T≤501 \le T \le 50.

1≤R≤3001 \le R \le 300.

1≤C≤3001 \le C \le 300.

0≤Vi,j≤1030 \le V_{i,j} \le 10^3 for all i,ji, j.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    Case #1: 2
    Case #2: 2
    Case #3: 6
    
  2. Example 2

    Input
    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
    
    Expected output
    Case #1: 4
    Case #2: 3
    Case #3: 4