Олимпиадный тренинг

Задача 21821. Tic-tac-toe


Задача

Темы:
Petya and Vasya became very bored at the biology lesson, and they decided to play the game of tic-tac-toe up to five in a row, beloved by all schoolchildren, on an endless field.
Let's briefly consider the rules of the game. The game takes place on an endless checkered field, two players make moves in turn, the first player moves with crosses, and the second — zeros. On your turn, the player
selects a free cell of the field and puts its character there. If after the move of the next player on the field there are five of his symbols in a row vertically, horizontally or diagonally, then the player who made such a move is declared the winner and the game ends.
Peter and Vasya have been playing the game for quite some time. Now Petya, who plays with crosses, should walk. Petya hopes to complete the game quickly and wants to win in no more than two, but better
in one move. Petya calls a move optimal if one of the following is true for this move:
• this move results in Petya's immediate victory;
• there is no move that leads to Petya's immediate victory, but if Petya makes this move, then Vasya will not win on the next move and, regardless of Vasya's response move, Petya has
there will be a next move that will lead to his immediate victory.
Help Petya find the number of optimal moves.
Input data format
The first input file contains two natural numbers n, m (1 ≤ n,m ≤ 200) — dimensions of the rectangle containing all the crosses and zeroes already placed on the field.
The next n lines contain m characters each, each equal to one of the following:
"." (dot), "X" (capital Latin letter "x") or "0" (zero). At the same time, «.» denotes an empty cell, "X" means a cross, and "0" means means zero. It is guaranteed that the field contains
Equal number of X's and O's and no player has won yet.
Output format
Print a single number — the number of Petit's optimal moves.
 
Examples
# Input Output
1
5 3
...
000
XXX
...
...
2
2
4 4
..0.
.XX0
.0X.
....
0
3
5 6
......
.XXX..
.0000.
..X...
......
0