#P510B. Fox And Two Dots

Fox And Two Dots

题面翻译

Fox Ciel 最近在手机上玩一个名叫 "Two Dots" 的游戏,这个游戏是在一个 n×mn\times m 的棋盘上玩的,如下图:

qwq

棋盘中的每个小方格中包含有一个点,并且这个点有特定的颜色。我们用大写字母 A 到 Z 来表示不同的颜色。 这个游戏的关键是要找一个有相同颜色的环。例如上图中,有 44 个蓝色点构成了一个环。

我们定义一个序列 d1,d2,,dkd_1,d_2,\cdots,d_k 是一个环,当且仅当满足下列条件:

  1. kk 个点在不同位置,也就是说,对于任何两个点,它们的位置不重复。
  2. k4k\geq 4
  3. 所有点的颜色相同。
  4. 对于所有的 1ik11\leq i\leq k-1did_idi+1d_{i+1} 相邻,当然 d1d_1dkd_k 也是相邻的。定义两个点相邻当且仅当他们有共同的边。

现在你的任务是判断这个棋盘里面是否有满足这种条件的环。

输入第一行包含两个正整数 nnmm,表示棋盘中行和列的数量 。

接下来 nn 行,每行输入 mm 个大写字符,表示棋盘中每个格子中小点点的颜色。注意是从字符 A 到字符 Z

如果有满足条件的环,输出 Yes,否则输出 No。

样例解释:

在第一个样例中,所有的 A 组成了一个环。

在第二个样例中没有满足条件的环。

第三个样例就是插图的例子。其中 Y 表示黄色,B 表示蓝色,R 表示红色。

题目描述

Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n×m n×m cells, like this:

Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1,d2,...,dk d_{1},d_{2},...,d_{k} a cycle if and only if it meets the following condition:

  1. These k k dots are different: if ij i≠j then di d_{i} is different from dj d_{j} .
  2. k k is at least 4.
  3. All dots belong to the same color.
  4. For all 1<=i<=k1 1<=i<=k-1 : di d_{i} and di+1 d_{i+1} are adjacent. Also, dk d_{k} and d1 d_{1} should also be adjacent. Cells x x and y y are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

输入格式

The first line contains two integers n n and m m ( 2<=n,m<=50 2<=n,m<=50 ): the number of rows and columns of the board.

Then n n lines follow, each line contains a string consisting of m m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

输出格式

Output "Yes" if there exists a cycle, and "No" otherwise.

样例 #1

样例输入 #1

3 4
AAAA
ABCA
AAAA

样例输出 #1

Yes

样例 #2

样例输入 #2

3 4
AAAA
ABCA
AADA

样例输出 #2

No

样例 #3

样例输入 #3

4 4
YYYR
BYBY
BBBY
BBBY

样例输出 #3

Yes

样例 #4

样例输入 #4

7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

样例输出 #4

Yes

样例 #5

样例输入 #5

2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

样例输出 #5

No

提示

In first sample test all 'A' form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).