F - Like As Shogi Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

9 行、横 9 列からなるマス目があり、上から i 行目、左から j 列目のマスを (i,j) と表すことにします。

はじめ、マス (A,B) にコマが一つ置かれています。その他のマスにはコマは置かれていません。

コマは、自身が存在するマスの 8 近傍に位置するマスのうちいくつかに一手で移動することができます。移動することのできるマスの集合の具体的な情報は長さがそれぞれ 3 である 3 つの文字列 S_1,S_2,S_3 によって表され、コマがあるマス (x,y) に存在するとき、

  • S_ij 文字目が # ならマス (x+i-2,y+j-2) に一手で移動することが可能
  • S_ij 文字目が . ならマス (x+i-2,y+j-2) に一手で移動することは不可能

です。ただし、マス目の外に出るような移動をすることはできません。

0 回以上の移動によってコマが辿り着けるマスの個数を求めてください。

制約

  • 1 \leq A,B \leq 9
  • S_1,S_2,S_3 はそれぞれ #. のみからなる長さ 3 の文字列
  • S_22 文字目は .
  • A,B は整数

入力

入力は以下の形式で標準入力から与えられる。

A B
S_1
S_2
S_3

出力

0 回以上の移動によってコマが辿り着けるマスの個数を出力せよ。


入力例 1

2 2
###
#..
...

出力例 1

5

コマはマス (1,1)、マス (1,2)、マス (1,3)、マス (2,1)、マス (2,2) の計 5 マスに辿り着くことができます。


入力例 2

5 4
###
#.#
###

出力例 2

81

コマは全てのマスに辿り着くことができます。


入力例 3

9 9
...
...
..#

出力例 3

1

はじめに置かれているマス (9,9) から他のマスに移動することができません。

Score : 7 points

Problem Statement

We have a grid with 9 rows arranged vertically and 9 columns arranged horizontally. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

Initially, there is a piece on (A, B), and no piece on the other squares.

In one move, the piece can move to some of the squares vertically, horizontally, or diagonally adjacent to the square it is on now. The specific set of squares that the piece can move to is described by three strings S_1, S_2, S_3 of length 3 each. When the piece is on some square (x, y),

  • if the j-th character of S_i is #, it can move to (x+i-2,y+j-2) in one move;
  • if the j-th character of S_i is ., it cannot move to (x+i-2,y+j-2) in one move.

Here, it is not allowed to exit the grid.

Find the number of squares that the piece can reach by zero or more moves.

Constraints

  • 1 \leq A,B \leq 9
  • Each of S_1,S_2,S_3 is a string of length 3 consisting of # and ..
  • The 2-nd character of S_2 is ..
  • A,B are integers.

Input

Input is given from Standard Input in the following format:

A B
S_1
S_2
S_3

Output

Print the number of squares that the piece can reach by zero or more moves.


Sample Input 1

2 2
###
#..
...

Sample Output 1

5

The piece can reach the following five squares: (1,1), (1,2), (1,3), (2,1), and (2,2).


Sample Input 2

5 4
###
#.#
###

Sample Output 2

81

The piece can reach all squares.


Sample Input 3

9 9
...
...
..#

Sample Output 3

1

The piece cannot move from the starting square (9, 9).