National Treasures

Time limit1sMemory limit128 MB

Summary
Given a grid of artifacts with bitmask critical points and cells already holding guards, replace some artifacts with hired guards so every remaining artifact has a guard on each of its critical points, minimizing hires.
Level

Hard8 of 10

Topics
Greedy, Minimum spanning tree, Brute force, Simulation
Solved
No attempts yet

Problem

The great hall of the national museum has been robbed several times recently, and everyone is now worried about the security of the treasures on display. To keep the hall safe, the museum hires additional guards to stand inside it and watch over the ancient artifacts. The museum wants to hire the minimum number of additional guards so that the whole hall is secured.

The great hall is a grid of R×CR \times C cells. Some cells are already occupied by the museum's own guards. Every other cell holds an artifact of some type (a statue, a sculpture, and so on). An artifact may be replaced by a newly hired guard, but a guard may never stand on a cell that still holds an artifact, and an artifact may never simply be taken away to leave its cell empty — for each artifact cell you either keep the artifact or swap it for a hired guard.

Each artifact has a set of critical points: cells that must have a guard standing on them if that artifact is to remain in the hall. One guard standing on a cell that is a critical point of several artifacts watches over all of them at once. The critical points of any artifact are always a subset of the 12 cells around it, numbered 1 through 12 as shown below (X marks the artifact itself, and each number marks the critical point with that number):

 .  2  .  3  .
 1  .  9  .  4
 . 12  X 10  .
 8  . 11  .  5
 .  7  .  6  .

The type of an artifact is a non-negative integer whose ii-th bit — counting from the least significant bit as bit 11 — is 11 exactly when critical point number ii is a critical point of that artifact. For example, an artifact of type 595595 (binary 10010100111001010011) has the critical points {1,2,5,7,10}\{1, 2, 5, 7, 10\}. If a critical point of an artifact lies outside the grid, it is automatically considered secure.

Given the layout of the great hall, find the minimum number of additional guards to hire so that every remaining artifact is secured.

Input

The input contains one or more test cases. Each test case begins with a line holding two integers RR and CC (1≤R,C≤501 \le R, C \le 50), the dimensions of the hall. Each of the next RR lines contains CC integers separated by one or more spaces. The jj-th integer of the ii-th line is −1-1 if cell (i,j)(i, j) already holds one of the museum's guards; otherwise it is an integer TT (0≤T<2120 \le T < 2^{12}) giving the type of the artifact in that cell.

The input ends with a line containing two zeros; this terminating line is not a test case.

Output

For each test case, print one line of the form:

k. G

where kk is the test case number (starting from 11) and GG is the minimum number of additional guards that must be hired so that every remaining artifact is secured.

Examples1

  1. Example 1

    Input
    1 3
    512 -1 2048
    2 3
    512 2560 2048
    512 2560 2048
    0 0
    
    Expected output
    1. 0
    2. 2