#P14814. [ICPC 2023 Yokohama R] Yokohama Phenomena

[ICPC 2023 Yokohama R] Yokohama Phenomena

题目描述

Do you know about Yokohama Phenomena? The phenomenon takes place when three programmers, sitting around a table, hold a single pen together above a board. A grid of squares is drawn on the board, with each square marked with a single letter. Although none of the participants purposely moves the pen, its nib, as if it has a will, goes down to one of the squares marked with Y, and then starts moving on the board. The squares passed are marked with O, K, O, H, A, and M in this order, and then the nib stops on the square marked with A.

Let us call the series of squares along such a trajectory of the nib a YOKOHAMA trace. A YOKOHAMA trace is defined as follows.

  • It is a series of eight squares in the given grid of squares.
  • Every square in the series, except for the first one, shares an edge with (is edge-adjacent to) its directly preceding square in the series.
  • The letters marked in the eight squares of the series are Y, O, K, O, H, A, M, and A, in this order.

Note that the same square may appear more than once in the series.

Figure A.1 (a) is an illustration of the board corresponding to Sample Input 1. Figures A.1 (b) and (c) show trajectories on two of the YOKOHAMA traces. Both traces start at the leftmost square in the upper row. The same square marked with O appears twice in the trace illustrated in Figure A.1 (c).

:::align{center}

Figure A.1. A board and trajectories on two of the YOKOHAMA traces :::

You are given a grid of squares, each marked with one of six letters, A, H, K, M, O, or Y. Your task is to count how many distinct YOKOHAMA traces are possible on it.

输入格式

The input consists of a single test case of the following format.

$$\begin{aligned} &n\ m \\ &x_{1,1}\ \cdots\ x_{1,m} \\ &\vdots \\ &x_{n,1}\ \cdots\ x_{n,m} \end{aligned}$$

The first two integers nn and mm (1n101 \le n \le 10, 1m101 \le m \le 10) describe the size of the grid. The grid has squares arranged in an n×mn \times m matrix. The following nn lines describe the letters marked in the squares. The square at the ii-th row and the jj-th column in the grid (1in1 \le i \le n, 1jm1 \le j \le m) has letter xi,jx_{i,j} marked in it. Each xi,jx_{i,j} is one of the six letters, A, H, K, M, O, or Y.

输出格式

Output a line containing the number of distinct YOKOHAMA traces.

2 4
YOHA
OKAM
8
3 4
YOKH
OKHA
KHAM
0
3 6
MAYOHA
AHOKAM
MAYOHA
80