Spinning Blade (Large)

Time limit5sMemory limit512 MB

Summary
Find the largest K by K square, with its four corner cells removed, whose cell masses balance exactly about the square center.
Level

Medium6 of 10

Topics
Prefix sum, Brute force
Solved
No attempts yet

Problem

You are building a trap for your hideout: a spinning blade. The blade is cut out of a heavy metal sheet that carries a painted grid of square cells, RR rows by CC columns. First you cut out a square of K×KK \times K cells aligned to the grid, where K≥3K \ge 3. Then you cut away the four 1×11 \times 1 corner cells of that square. What is left is the blade.

Every cell was supposed to have mass DD, but the sheet is not perfectly uniform, so the cell in row ii and column jj has mass D+wijD + w_{ij}. The shaft goes exactly through the center of the K×KK \times K square, so the blade spins properly only if its center of mass sits exactly at that center.

Given the grid and the mass of every cell, find the largest KK such that some K×KK \times K square inside the sheet gives a blade whose center of mass is exactly at the center of the square.

Input

The first line contains the number of test cases TT. Each test case starts with a line of three integers RR, CC and DD: the number of rows, the number of columns, and the mass you expected each cell to have. The next RR lines each contain CC digits with no spaces. The jj-th digit of the ii-th line is wijw_{ij}, the difference between the actual mass and the expected mass of that cell. Each cell has uniform density, and its mass is an integer between DD and D+9D + 9, inclusive.

Limits

  • 1≤T≤201 \le T \le 20
  • 3≤R≤5003 \le R \le 500
  • 3≤C≤5003 \le C \le 500
  • 1≤D≤1061 \le D \le 10^6
  • 0≤wij≤90 \le w_{ij} \le 9
  • The size of the input is at most 625KB.

Output

For each test case, print one line of the form Case #x: K, where xx is the test case number starting from 1 and KK is the largest blade size you can cut out. If no blade of size 3 or more has its center of mass at the center of its square, print IMPOSSIBLE in place of KK.

Note

The center of mass of a flat object is the point cc with this property: adding up (p−c) mass(p)(p - c)\,\mathrm{mass}(p) over all points pp of the object gives the zero vector. Here pp, cc and the zero are two dimensional vectors. The definition still works when each grid cell is treated as a single point that carries all of its mass at the center of the cell.

For an example, take a 3×33 \times 3 sheet with D=7D = 7 whose digit rows are 123, 234 and 345 from top to bottom. The only candidate blade is the whole square with its four corner cells cut away. Put the origin at the bottom left corner of the sheet, let xx grow to the right and yy grow up, so every cell center lands on half integer coordinates. The five remaining cells have masses 9, 9, 10, 11 and 11, and their center of mass is (1.54,1.46)(1.54, 1.46):

(−1.04,0.04)×9+(−0.04,1.04)×9+(−0.04,0.04)×10+(−0.04,−0.96)×11+(0.96,0.04)×11=(0,0)(-1.04, 0.04) \times 9 + (-0.04, 1.04) \times 9 + (-0.04, 0.04) \times 10 + (-0.04, -0.96) \times 11 + (0.96, 0.04) \times 11 = (0, 0)

The center of the square is (1.5,1.5)(1.5, 1.5), so this blade is not balanced.

Examples1

  1. Example 1

    Input
    2
    6 7 2
    1111111
    1122271
    1211521
    1329131
    1242121
    1122211
    3 3 7
    123
    234
    345
    
    Expected output
    Case #1: 5
    Case #2: IMPOSSIBLE