This page is still under construction.

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

Inventor Outlasting

Time limit40sMemory limit1024 MB

Summary
Two players take turns building attractions on free X cells, where each build fills diagonal sign runs; count the first moves that let the first player win.
Level

Hard8 of 10

Topics
Game theory, Simulation, Matrix
Solved
No attempts yet

Problem

Izabella and Olga take turns playing a new game. In this game, they play attraction inventors working for a theme park. The board is a matrix of square cells that shows the map of the theme park. Some cells are suitable for a new attraction.

When an attraction is built, signs advertising it are added automatically. Four sign builders are sent out, one in each diagonal direction. The builder moving north-east works as follows: starting from the cell where the attraction is built, check the cell north-east of the current cell. If there is no such cell, or the cell is occupied, the builder stops. Otherwise, the builder moves to that cell, builds a sign there, and repeats the process. The builders moving north-west, south-east, and south-west work the same way, only in their own directions. A cell is occupied if it contains either an attraction or a sign.

For example, the left picture below shows a map, where yellow cells are spots where attractions can be built. After an attraction is built at 3,43,4 (marked with a blue square), signs are built on the cells marked in gray. Some spots that were available for attractions are no longer available because they now contain a sign. After a second attraction is built at 3,63,6, its new sign builders only go as far as an existing sign, which leads to the situation in the right picture.

On a turn, a player can build an attraction on any available spot. The sign builders then proceed automatically, which may make other spots unavailable, as in the example. The goal is to outlast the opponent. The player who has no available spot to build an attraction on their turn loses.

Izabella plays first. Assuming both players play optimally to win, count how many different first moves for Izabella would let her win.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers RR and CC, the number of rows and columns of the board. Then RR lines follow. The ii-th of these lines contains a string of CC characters Li,1Li,2⋯Li,CL_{i,1}L_{i,2}\cdots L_{i,C}. Li,jL_{i,j} is an uppercase X if the cell in row ii and column jj is a spot available for a new attraction, and a period (.) if it is not.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the number of first moves for Izabella that result in her winning the game.

Limits

  • 1≤T≤1001 \le T \le 100.
  • Li,jL_{i,j} is either an uppercase X or a period (.), for all i,ji,j.

Hints

In Sample Case #1, the only winning move for Izabella is to place her attraction at (2,4)(2,4). The other 66 moves lead to a game that Olga wins if both players play optimally.

In Sample Case #2, either valid first move for Izabella leaves a board with 22 remaining spots. Either of the 22 remaining spots Olga can then choose leaves a board with 11 remaining spot, which is a winning position for Izabella. So all 33 valid first moves win.

In Sample Case #3, only the middle of the 55 spots is a winning first move for Izabella. The other 44 moves lead to a game that Olga can win.

In Sample Case #4, either valid first move leaves Olga with no valid move, so Izabella wins immediately with any of them.

Examples1

  1. Example 1

    Input
    4
    5 7
    .......
    ...X.X.
    ...X.X.
    ..XX...
    ..X....
    1 5
    X.X.X
    2 5
    X.X.X
    .X.X.
    2 2
    X.
    .X
    
    Expected output
    Case #1: 1
    Case #2: 3
    Case #3: 1
    Case #4: 2