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

[Image]

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 d_1, d_2, ..., d_{k} a cycle if and only if it meets the following condition:

  These k dots are different: if i ≠ j then d_{i} is different from d_{j}.  k is at least 4.  All dots belong to the same color.  For all 1 ≤ i ≤ k - 1: d_{i} and d_{i} + 1 are adjacent. Also, d_{k} and d_1 should also be adjacent. Cells x and y are called adjacent if they share an edge. 

Determine if there exists a cycle on the field.

-----Input-----

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

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

-----Output-----

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

-----Examples-----
Input
3 4
AAAA
ABCA
AAAA

Output
Yes

Input
3 4
AAAA
ABCA
AADA

Output
No

Input
4 4
YYYR
BYBY
BBBY
BBBY

Output
Yes

Input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

Output
Yes

Input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

Output
No


-----Note-----

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).

=====
Write me a Python program to find a cycle that contain dots of same color?
assistant
#Here is Python program to find a cycle that contain dots of same color.

#f = lambda: input()
#file = open("btest", "r")
#f = lambda:file.readline()

# n, m = map(int, f().split())

# g = []

# for i in range(n):
#     g.append(f())

# disco = [[False for _ in range(m)] for _ in range(n)]

# #print(disco)
# #print(parent)
          
# def found_cycle(w):
#     nonlocal disco
#     s = []
#     s.append(w)
#     while s:
#         v = s.pop()
#         if disco[v[0]][v[1]] == False:
#             if v[0] > 0 and g[v[0]-1][v[1]] == g[v[0]][v[1]]:
#                 s.append([v[0]-1, v[1]])
                
#             if v[0] < n-1 and g[v[0]+1][v[1]] == g[v[0]][v[1]]:
#                 s.append([v[0]+1, v[1]])
                
#             if v[1] > 0 and g[v[0]][v[1]-1] == g[v[0]][v[1]]:
#                 s.append([v[0], v[1]-1])
                
#             if v[1] < m-1 and g[v[0]][v[1]+1] == g[v[0]][v[1]]:
#                 s.append([v[0], v[1]+1])
                
#         else:
#             disco[v[0]][v[1]] = True
#     return False

# found = False
# for i in range(n):
#     if found:
#         break
#     for j in range(m):
#         if disco[i][j] == False:
#             found = found_cycle([i,j])
#             break

# if found:
#     print ("Yes")
# else:
#     print ("No")

#Editorial
import sys
sys.setrecursionlimit(10000)

#file = open("btest", "r")
#f = lambda:file.readline()
f = lambda:input()

n,m = list(map(int, f().split()))

matrix = []

disco = [[0 for _ in range(m)] for _ in range(n)]

for i in range(n):
    matrix.append(f())

def valid_node(i, j):
    return 0 <= i < n and 0 <= j < m

def dfs(i, j, p, k, cc = 0):
    if disco[i][j] == k:
        return True
    
    disco[i][j] = k

    res = False
    dd = [[0,1], [1,0], [-1,0], [0,-1]]
    for d in dd:
        if valid_node(i+ d[0], j+d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]):
            if matrix[i + d[0]][j+ d[1]] == matrix[i][j]:
                res = res or dfs(i + d[0], j+d[1], [i,j], k, cc+1)

    return res

ok = False
k = 1
for i in range(n):
    for j in range(m):
        if...