Clever Cell Choices
Time limit6sMemory limit1024 MB
Count the empty cells where the first player, placing the opening stone there, wins the game of alternately placing stones on side-adjacent empty cells.
Problem
Two players play the following game on an grid:
- Initially each cell is either empty or occupied.
- Players take turns placing a stone on an empty cell, occupying it. Each new stone must be placed on a cell that shares a side with the last placed stone. The starting stone may be placed on any empty cell.
- A player who cannot place a stone by these rules loses, and the other player wins.
A winning starting cell is a cell where the first player places the starting stone and wins when both players play optimally. Given the initial grid, count the winning starting cells.
Input
The first line contains two integers and (), the dimensions of the grid. Each of the next lines contains a string of length . The -th character of the -th string describes cell : . is an empty cell and # is a cell with a stone.
Output
Output a single integer, the number of winning starting cells.