Under construction

Still being built. Things may change

Cairo Corridor

Time limit2sMemory limit512 MB

Summary
Each tile is split into two pentagon halves; find the connected clear region touching all four borders and decide whether it is minimal.
Level

Medium7 of 10

Topics
Graph, BFS, Implementation, Simulation
Solved
No attempts yet

Problem

The Cairo pentagonal tiling covers the plane with semiregular pentagons. It is named after Cairo, where several streets are paved with variations of the pattern.

Consider a bounded tiling in which every pentagon is either clear (white) or filled in (grey). A corridor is a maximal set of clear pentagons, connected to one another, that reaches all four borders of the tiling. Two pentagons are adjacent when they share an edge; sharing a single corner is not enough. A tiling has at most one corridor. A corridor is minimal when it has no superfluous pentagon, that is, when filling in any one pentagon of the corridor leaves a set that is no longer a corridor.

The figure shows four tilings. The first three have a corridor, drawn in yellow. The corridors of (a) and (b) are minimal, the corridor of (c) is not: fill in the tiles marked X, among others, and a corridor still exists. The rightmost tiling has no corridor. Tilings (a) and (c) are the two tilings of the first example.

For each tiling, decide whether a corridor exists and is minimal. When it is minimal, report the size of the corridor, that is, the number of clear pentagons in it.

Input

The first line contains the number of tilings TT. Each tiling starts with a line holding two integers NN and MM separated by a space. The next NN lines each contain 2M2M binary digits that give the pairs of pentagons aija_{ij}, bijb_{ij}, where 0 is a clear pentagon and 1 is a filled one. Line ii lists ai1a_{i1}, bi1b_{i1}, ai2a_{i2}, bi2b_{i2}, and so on up to biMb_{iM}.

Read the tiling as a grid of N×MN \times M squares. The square in row ii and column jj holds one pair of pentagons. If i+ji + j is even, aija_{ij} is the left half of that square and bijb_{ij} is the right half. If i+ji + j is odd, aija_{ij} is the top half and bijb_{ij} is the bottom half. Horizontal pairs and vertical pairs therefore alternate like the squares of a checkerboard. Two pentagons are adjacent when their halves share a segment of positive length, and a pentagon reaches a border of the tiling when its half touches that side of the outer rectangle.

Output

Print TT lines. Line kk holds the size of the corridor of the kk-th tiling when that tiling has a minimal corridor, and NO MINIMAL CORRIDOR otherwise. Print the text NO MINIMAL CORRIDOR exactly as written.

Constraints

  • 1≤T≤101 \le T \le 10, the number of tilings
  • 1≤Nk1 \le N_k and 1≤Mk1 \le M_k for every tiling kk
  • ∑k=1TNk≤250\sum_{k=1}^{T} N_k \le 250, the total number of grid lines
  • ∑k=1TMk≤250\sum_{k=1}^{T} M_k \le 250, the total number of tile pairs per line

Examples2

  1. Example 1

    Input
    2
    6 6
    010101001001
    001000101100
    110101001101
    010010000100
    001110110010
    001001101010
    6 6
    010010110010
    001100100111
    000110100101
    011001100101
    100100011100
    011010001101
    
    Expected output
    17
    NO MINIMAL CORRIDOR
    
  2. Example 2

    Input
    1
    3 4
    11110111
    01000000
    11110111
    
    Expected output
    9