Harryxun/stf_run huggingface dataset:
h, w = map(int, input().split())
hwl = []
for _ in range(h):
    l = list(input())
    hwl.append(l)

for i in range(h):
    for j in range(w):
        if hwl[i][j] == ".":
            cnt = 0
            if i != 0 and j != 0 and hwl[i-1][j-1] == "#":
                cnt += 1
            if i != 0 and hwl[i-1][j] == "#":
                cnt += 1
            if i != 0 and j < w-1 and hwl[i-1][j+1] == "#":
                cnt += 1
            if j != 0 and hwl[i][j-1] == "#":
                cnt += 1
            if j < w-1 and hwl[i][j+1] == "#":
                cnt += 1
            if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
                cnt += 1
            if i < h-1 and hwl[i+1][j] == "#":
                cnt += 1
            if i < h-1 and j < h-1 and hwl[i+1][j+1] == "#":
                cnt += 1
            hwl[i][j] = str(cnt)
    print("".join(hwl[i]))


### FIXED CODE ###

h, w = map(int, input().split())
hwl = []
for _ in range(h):
    l = list(input())
    hwl.append(l)

for i in range(h):
    for j in range(w):
        if hwl[i][j] == ".":
            cnt = 0
            if i != 0 and j != 0 and hwl[i-1][j-1] == "#":
                cnt += 1
            if i != 0 and hwl[i-1][j] == "#":
                cnt += 1
            if i != 0 and j < w-1 and hwl[i-1][j+1] == "#":
                cnt += 1
            if j != 0 and hwl[i][j-1] == "#":
                cnt += 1
            if j < w-1 and hwl[i][j+1] == "#":
                cnt += 1
            if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
                cnt += 1
            if i < h-1 and hwl[i+1][j] == "#":
                cnt += 1
            if i < h-1 and j < w-1 and hwl[i+1][j+1] == "#":
                cnt += 1
            hwl[i][j] = str(cnt)
    print("".join(hwl[i]))
N = int(input())
A = list(map(int,input().split()))

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

print(gcd_list(A))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))

from fractions import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)

print(gcd_list(A))
N, K = [int(x) for x in input().split()]

t1 = abs(N - K)
t2 = K // t1

if t2 > N:
    print(N)
else:
    print(t2)

### FIXED CODE ###

def main():
    N, K = list(map(int, input().split()))
    m = N % K
    m = min(m, abs(K-m))
    print(m)
 
if __name__ == '__main__':
    main()
x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisc.sort(reverse=True)
an,ans = [],[]
for num in lisb:for nu in lisa:an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:for nu in an:ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):print(ans[i])

### FIXED CODE ###

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for num in lisb:
    for nu in lisa:
        an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:
    for nu in an:
        ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])
hands_a, hands_b, hands_c = (list(input()) for i in range(3))

hands = {'a': hands_a, 'b': hands_b, 'c': hands_c}

turn = 'a'
while len(hands[turn]) > 0:
    turn = hands[turn].popleft()
print(turn.upper())


### FIXED CODE ###

hands_a, hands_b, hands_c = (input() for i in range(3))

hands = {'a': hands_a, 'b': hands_b, 'c': hands_c}

turn = 'a'
while True:
    if len(hands[turn]) == 0:
        print(turn.upper())
        exit()
    next_turn = hands[turn][0]
    hands[turn] = hands[turn][1:]
    turn = next_turn
s = list(input())
s_ = list('abcdefghijklmnopqrstuvwxyz')

s = list(set(s))
s.sort()

for i in range(26):
    if s[i] != s_[i]:
        print(s_[i])
        exit()
print('None')

### FIXED CODE ###

s = list(input())
s_ = list('abcdefghijklmnopqrstuvwxyz')

s = list(set(s))
s.sort()

for i in range(len(s)):
    if s[i] != s_[i]:
        print(s_[i])
        exit()
if len(s) < 26: print(s_[len(s)])
else: print('None')
N,K,S = map(int, input().split())

sa = N-K+1
a = [1 for i in range(N)]

if S-sa+1 >0:
        
    a[0] = S - sa + 1

    for i in range(1,sa):
        a[i] = 1
    j = 0
    for i in range(sa,N):
        a[i] = a[j]

        j +=1
        j %= 3

    for x in a:
        print(x, end = " ")
else:
    for i in range(K):
        a[i] = S
    if S == 1:
        for i in range(K,N):
            a[i] = 2
    for x in a:
        print(x, end = " ")

### FIXED CODE ###

N,K,S = map(int, input().split())

sa = N-K+1
a = [1 for i in range(N)]
if S-sa+1 >0:
        
    a[0] = S - sa + 1

    for i in range(1,sa):
        if i < N:
    
            a[i] = 1
    j = 0
    for i in range(sa,N):
        a[i] = a[j]

        j +=1
        j %= sa

    for x in a:
        print(x, end = " ")


else:
    for i in range(K):
        a[i] = S
    if S != 10**9:    
        for i in range(K,N):
            a[i] = 10**9
    else:
        for i in range(K,N):
            a[i] == 10**9 -1
    for x in a:
        print(x, end = " ")
N = int(input())

A = list(map(int, input().split()))

import statistics as st

print(st.harmonic_mean(A)/N)

### FIXED CODE ###

N = int(input())

A = list(map(int, input().split()))

import numpy as np
A = np.array([A])
print(1/np.sum(1/A))
x, n = map(int, input().split())
p = list(map(int, input().split()))

l = list(range(1, 101))

p = list(set(l)-set(p))
sa = [abs(x - p[i]) for i in range(len(p))]
sa_min = min(sa)

for i in range(len(l)):
    if sa[i] == sa_min:
        print(p[i])
        break

### FIXED CODE ###

x, n = map(int, input().split())
p = list(map(int, input().split()))

cnt = 0
while True:
    if x+cnt not in p:
        print(x+cnt)
        break
    cnt *= -1
    if x+cnt not in p:
        print(x+cnt)
        break
    cnt *= -1
    cnt -= 1
import string
def main():
    s = input()
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    for i in "abcdefghijklmnopqrstuvwxyz@":
        if i == "@":
            t = 0
            for x in range(len(s), 0, -1):
                for y in range(x-1, -1, -1):
                    if x == y:
                        continue
                    if s[x] > s[y]:
                        print(s.translate(string.maketrans(s[x],x[y])))
                        return 
        if s.count(i) == 0:
            print (s + i)
            break

main()



### FIXED CODE ###

import string

alp = string.ascii_lowercase

def main():
    s = input()
    if len(s) < 26:
        for i in alp:
            if i not in s:
                print (s + i)
                return
    if s == alp[::-1]:
        print(-1)
        return
    L = len(s)
    tmp = []
    tt = ''
    for i, ss in enumerate( s ):
        tmp += [ s[L-i-1] ]
        if s[L-i-1] > s[L-i-2]:
            ans = s[:L-i-2]
            tt = s[L-i-2] 
            break
    tmp.sort()
    for i in tmp:
        if i > tt:
            print (ans + i)
            break
main()
n,k = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(k,n+1):
  c = a[i-k:i+1]
  d = 1
  for j in range(k):
    d *= c[j]
  b.append(d)
for i in range(len(d)-1):
  if d[i+1]-d[i] > 0:
    print("Yes")
  else:
    print("No")

### FIXED CODE ###

n,k = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n-k):
  if a[i+k] > a[i]:
    print("Yes")
  else:
    print("No")
a=input()
b=input()
if a*b%2==0:
  print('Even')
else:
  print('Odd')

### FIXED CODE ###

a, b = map(int, input().split())
if a*b%2==0:
  print('Even')
else:
  print('Odd')
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    C = [set() for _ in range(N)]
    E = [[] for _ in range(N)]
    E2 = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        E[a-1].append(b-1)
        E[b-1].append(a-1)
        E2.append([a-1, b-1])
#    print(E)
#    print(E2)
    K = 0
    for e in E:
        K = max(K, len(e))
    print(K)
    for a, b in E2:
        s = set()
        for i in range(1, K+1):
            s.add(i)
        s -= C[a]
        s -= C[b]
        c = s.pop()
        print(c)
        C[a].add(c)
        C[b].add(c)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input = sys.stdin.readline


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        G[a-1].append((b-1, i))
        G[b-1].append((a-1, i))

    V = [[-1, -1] for _ in range(N)]  # 頂点の[親, 親への辺の色]
    ans = [-1]*(N-1)
    import collections
    Q = collections.deque()
    Q.append(0)
    while Q:
        i = Q.popleft()
        color = 0
        for c, ei in G[i]:
            if c == V[i][0]:
                continue
            V[c][0] = i
            color += 1
            if color == V[i][1]:
                color += 1
            V[c][1] = ans[ei] = color
            Q.append(c)
    print(max(ans))
    print('\n'.join(map(str, ans)))


if __name__ == '__main__':
    main()
N = int(input())
H = map(int, input().split())
H = list(H)
res = 'Yes'
for i in range(len(H) - 1):
  for j in range(i + 1, len(H)):
    if H[i] - H[j] >= 2:
      res = 'No'
      break
  if res == 'No':
    break
print(res)


### FIXED CODE ###

A, B, C = map(int, input().split())
if A - B >= C:
  r = 0
else:
  r = C - (A - B)
print(r)
N, x = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
 
def counter(x, alist):
  count = 0
  while x > 0:
      if alist:
          tmp = alist.pop(0)
          x = x - tmp
          if x == 0:
              return count + 1
          elif x > 0:
              count += 1
          else:
              return count
      else:
          return count - 1
print(counter(x, alist))
N, x = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()

def counter(x, alist):
  count = 0
  while x > 0:
      if alist:
          tmp = alist.pop(0)
          x = x - tmp
          if x == 0:
              return count + 1
          elif x > 0:
              count += 1
          else:
              return count
      else:
          return count - 1
print(counter(x, alist))

### FIXED CODE ###

N, x = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
 
def counter(x, alist):
  count = 0
  while x > 0:
      if alist:
          tmp = alist.pop(0)
          x = x - tmp
          if x == 0:
              return count + 1
          elif x > 0:
              count += 1
          else:
              return count
      else:
          return count - 1
print(counter(x, alist))
a,b = map(input().split())
print(math.ceil(((b-1)/(a-1))))

### FIXED CODE ###

import math
a,b = map(int, input().split())
print(math.ceil(((b-1)/(a-1))))
g_m=0
g_G=[]
g_cnt=0
A=[]
n = int(input())
for i in range(n):
    A.append(int(input()))
def insertionSort(A,n,g):
    global g_cnt
    for i in range(g,n):
        v = A[i]
        j = i - g
        while j >= 0 and A[j] > v:
            A[j+g] = A[j]
            j = j-g
            g_cnt += 1
        A[j+g]=v
    


def shellSort(A,n):
    cnt = 0
    m = int(n**0.5)
    g_m=m
    G=[]
    for i in range(1,n,3*i):
        G.append(i)
    g_G=G
    for i in range(0,m):
        insertionSort(A,n,G[i])
    return A,m,G
A,g_m,g_G=shellSort(A,n)
print(g_m)
print(" ".join(map(str,g_G)))
print(g_cnt)
print(" ".join(map(str,A)))

### FIXED CODE ###

g_m=0
g_G=[]
g_cnt=0
A=[]
n = int(input())
for i in range(n):
    A.append(int(input()))
def insertionSort(A,n,g):
    global g_cnt
    for i in range(g,n):
        v = A[i]
        j = i - g
        while j >= 0 and A[j] > v:
            A[j+g] = A[j]
            j = j-g
            g_cnt += 1
        A[j+g]=v
    
def shellSort(A,n):
    m=1
    G=[]
    j=1
    for i in range(0,100):
        G.append(j)
        j=3*j+1
    
    while G[m]<=n:
        m+=1
    G=G[:m]
    G.reverse()
    for i in range(0,m):
        insertionSort(A,n,G[i])
    return A,m,G
A,g_m,g_G=shellSort(A,n)
print(g_m)
print(" ".join(map(str,g_G)))
print(g_cnt)
print(*A,sep="\n")
def main():
    top = list(map(int, input().split()))
    middle = list(map(int, input().split()))
    bottom = list(map(int, input().split()))
    N = int(input())
    taken = []
    for i in range(N):
        taken.append(int(input()))
    
    bingo = []
    bingo.append(top)
    bingo.append(middle)
    bingo.append(bottom)
    temp = [top[0], middle[0], bottom[0]]
    bingo.append(temp)
    temp = [top[1], middle[1], bottom[1]]
    bingo.append(temp)
    temp = [top[2], middle[2], bottom[2]]
    bingo.append(temp)
    temp = [top[0], middle[1], bottom[2]]
    bingo.append(temp)
    temp = [top[2], middle[1], bottom[0]]
    bingo.append(0)
 
 
    cnt = 0
    flag = 0
    for i in range(len(bingo)):
        cnt = 0
        for j in range(len(bingo[i])):
            if bingo[i][j] in taken:
                cnt += 1
        if cnt == 3:
            flag += 1
    
    if flag > 0:
        print('Yes')
    else:
        print('No')
main()

### FIXED CODE ###

def main():
    top = list(map(int, input().split()))
    middle = list(map(int, input().split()))
    bottom = list(map(int, input().split()))
    N = int(input())
    taken = []
    for f in range(N):
        taken.append(int(input()))
    
    bingo = []
    bingo.append(top)
    bingo.append(middle)
    bingo.append(bottom)
    temp = [top[0], middle[0], bottom[0]]
    bingo.append(temp)
    temp = [top[1], middle[1], bottom[1]]
    bingo.append(temp)
    temp = [top[2], middle[2], bottom[2]]
    bingo.append(temp)
    temp = [top[0], middle[1], bottom[2]]
    bingo.append(temp)
    temp = [top[2], middle[1], bottom[0]]
    bingo.append(temp)
    
    if bingo[0][0] in taken and bingo[0][1] in taken and bingo[0][2] in taken:
        print('Yes')
    elif bingo[1][0] in taken and bingo[1][1] in taken and bingo[1][2] in taken:
        print('Yes')
    elif bingo[2][0] in taken and bingo[2][1] in taken and bingo[2][2] in taken:
        print('Yes')
    elif bingo[3][0] in taken and bingo[3][1] in taken and bingo[3][2] in taken:
        print('Yes')
    elif bingo[4][0] in taken and bingo[4][1] in taken and bingo[4][2] in taken:
        print('Yes')
    elif bingo[5][0] in taken and bingo[5][1] in taken and bingo[5][2] in taken:
        print('Yes')
    elif bingo[6][0] in taken and bingo[6][1] in taken and bingo[6][2] in taken:
        print('Yes')
    elif bingo[7][0] in taken and bingo[7][1] in taken and bingo[7][2] in taken:
        print('Yes')
    else:
        print('No')
main()
x, y, z, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
r = [0] * (x * y * z)
for i in range(x):
  for j in range(y):
    for l in range(z):
      r[l+j*y+i*x*y] = a[i] + b[j] + c[l]
r = sorted(r)
for i in range(k):
  print(r[-(i+1)])


### FIXED CODE ###

import numpy as np

x, y, z, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
r = []
for i in range(x):
  for j in range(y):
    if (i + 1) * (j + 1) > k:
        break
    for l in range(z):
      if (i + 1) * (j + 1) * (l + 1) > k:
        break
      r.append(a[i] + b[j] + c[l])
r.sort()
for i in range(k):
  print(r[-(i+1)])
a=list(input().split())
b=int(a[0])
c=int(a[1])
if b>c:
  print(b+(b-1))
elif c>b:
  print(c+(c-1))
elif b=c:
  print(2*b)

### FIXED CODE ###

q=list(input().split())
a=int(q[0])
b=int(q[1])
for i in range(2):
  if a>=b:
    if i==0:
      r=a
    else:
      v=a
    a=a-1
  else:
    if i==0:
      r=b
    else:
      v=b
    b=b-1
g=int(r+v)
print(g)
a,v = map(int,input().split())
b,w = map(int,input().split())
t = input()

move_a = int(v) * int(t) + int(a)
move_b = int(w) * int(t) + int(b)

if move_a >= move_b:
    print("YES")
else:
    print("NO")


### FIXED CODE ###

s = input()
print(s[0] + s[1] + s[2])
X = [str(input()) for _ in range(5)]
endzero = 0
elesnum = []
for x in X:
  if x[-1] == "0":
    endzero += int(x)
  else:
    elesnum.append(x)
elesnum.sort(key = lambda x: x[-1])
min_ = int(elesnum[0])
for e in elesnum[1:]:
  endzero += (int(e)//10+1)*10
print(endzero+min_)

### FIXED CODE ###

X = [input() for _ in range(5)]
endzero = 0
elesnum = []
for x in X:
  if x[-1] == "0":
    endzero += int(x)
  else:
    elesnum.append(x)
if elesnum:
  elesnum.sort(key = lambda x: x[-1])
  min_ = int(elesnum[0])
  for e in elesnum[1:]:
    endzero += (int(e)//10+1)*10
  print(endzero+min_)
else:
  print(endzero)
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

def solve(N: int, M: int, a: "List[int]"):
    if(len(a) >= 2):
        l = [a[i+1]-a[i] for i in range(M-1)]
        if(len(l) and min(l)<=1):
            print(0)
            exit()

    dp = [1] * (N+1)
    dp[1] = 1
    dp[2] = 2
    x = 0
    # if(M == 0):
    #     for i in range(2,N+1)
    
    for i in range(2,N+1):
        if(len(a) and a[x] == i-1):
            dp[i] = dp[i-2] % MOD
            # x = min(x+1,M-1)
        elif(len(a) and a[x] == i-2):
            dp[i] = dp[i-1] % MOD
            x = min(x+1,M-1)
        else:
            dp[i] = (dp[i-1] + dp[i-2]) % MOD
    
    print(dp[N])
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, a)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

def solve(N: int, M: int, a: "List[int]"):
    if(len(a) >= 2):
        l = [a[i+1]-a[i] for i in range(M-1)]
        if(len(l) and min(l)<=1):
            print(0)
            exit()

    dp = [1] * (N+1)
    dp[1] = 1
    if(N >= 2):
        dp[2] = 2
    x = 0
    # if(M == 0):
    #     for i in range(2,N+1)
    
    for i in range(2,N+1):
        if(len(a) and a[x] == i-1):
            dp[i] = dp[i-2] % MOD
            # x = min(x+1,M-1)
        elif(len(a) and a[x] == i-2):
            dp[i] = dp[i-1] % MOD
            x = min(x+1,M-1)
        else:
            dp[i] = (dp[i-1] + dp[i-2]) % MOD
    
    print(dp[N])
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, a)

if __name__ == '__main__':
    main()
A, B, K = map(int, input().split())

for k in range(K):

    if A % 2 != 0 and A < 1:
            
        A -= 1

    B += round(A/2)
    A -= round(A/2)

    if B % 2 != 0 and if B < 1:
            
        B -= 1

    A += round(B/2)
    B -= round(B/2)

print(A, B)

### FIXED CODE ###

S = input()

if len(S) == 2:

    print(S)

elif len(S) == 3:

    print(S[::-1])
if x == 0:
  print(1)
  
else:
  print(0)

### FIXED CODE ###

x = int(input())

if x == 1:
  print(0)
 
else:
  print(1)
N = int(input()) #要素数
A = list(map(int,input().split())) #ソート対象（スペース区切り→配列に展開）
 
flag = 1
i = 0
cnt = 0
 
#Sort処理（自分メモ：flagが0になったらループを抜ける）
while flag:
    flag = 0
    for j in reversed(range(i+1,N)):
        if A[j] < A[j-1]:
            tmp = A[j-1]
            A[j-1] = A[j]
            A[j] = tmp
            flag = 1
            cnt += 1
    i += 1
 
print(' '.join(map(str,A)))
print(cnt)


### FIXED CODE ###

def BS(A, N):
    A = list(map(int, A.split()))
    flag = 1
    count = 0 
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                unk = A[j]
                A[j] = A[j-1]
                A[j-1] = unk
                flag = 1
                count = count +1
    print(" ".join(map(str,A)))
    print(count)
 
N = int(input())
A = input()
BS(A, N)
import numpy as np
from collections import deque

def resolve():
    H, W = map(int, input().split())

    maze = []
    for i in range(H):
        maze.append(input())

    def dfs(st):

        INF = -1
        d = np.full((H, W), INF)

        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]

        que = deque()
        que.append(st)
        d[st[1]][st[0]] = 0

        while len(que):
            p = que.popleft()
            # print(p)

            for i in range(4):
                nx = p[0] + dx[i]
                ny = p[1] + dy[i]

                if nx >= 0 and nx < W and ny >= 0 and ny < H and maze[ny][nx] != "#" and d[ny][nx] == INF:
                    # print("next is ", nx, ny)
                    que.append([nx, ny])
                    d[ny][nx] = d[p[1]][p[0]] + 1
                    # print(d)
                    # print(que)

        cost = np.max(d)
        idx = np.unravel_index(np.argmax(d), d.shape)
        # print(idx)
        # print(cost)
        return idx, cost

    for y in range(H):
        for x in range(W):
            if maze[y][x] == '.':
                st = [x, y]
                break
        break
    
    idx, cost = dfs(st)
    # print("mid")
    # print(idx)
    idx, cost = dfs((idx[1], idx[0]))
    print(int(cost))

if __name__ == "__main__":
    resolve()

### FIXED CODE ###

import numpy as np
from collections import deque

def resolve():
    H, W = map(int, input().split())

    maze = []
    for i in range(H):
        maze.append(input())

    def bfs(st):

        INF = -1
        d = np.full((H, W), INF)

        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]

        que = deque()
        que.append(st)
        d[st[1]][st[0]] = 0

        while len(que):
            p = que.popleft()
            # print(p)

            for i in range(4):
                nx = p[0] + dx[i]
                ny = p[1] + dy[i]

                if nx >= 0 and nx < W and ny >= 0 and ny < H and maze[ny][nx] != "#" and d[ny][nx] == INF:
                    # print("next is ", nx, ny)
                    que.append([nx, ny])
                    d[ny][nx] = d[p[1]][p[0]] + 1
                    # print(d)
                    # print(que)

        cost = np.max(d)
        # idx = np.unravel_index(np.argmax(d), d.shape)
        # print(idx)
        # print(cost)
        return cost

    max_cost = 0
    for y in range(H):
        for x in range(W):
            if maze[y][x] == '.':
                st = [x, y]
                cost = bfs(st)
                if cost > max_cost:
                    max_cost = cost 
    
    # print(maze[1])
    # idx, cost = bfs(st)
    # idx, cost = bfs((idx[1], idx[0]))
    print(int(max_cost))

if __name__ == "__main__":
    resolve()
S = [input() for _ in range(3)]
for i in range(3):
    S[i] = list(S[i])
p = 0 #プレイヤー
while True:
    if not S[0] and p == 0:
        print("A")
        break
    if not S[1] and p == 1:
        print("B")
        break
    if not S[2] and p == 3:
        print("C")
        break
    p_t = S[p].pop(0)
    if p_t == "a":
        p = 0
    elif p_t == "b":
        p = 1
    elif p_t == "c":
        p = 2

### FIXED CODE ###

S = [input() for _ in range(3)]
for i in range(3):
    S[i] = list(S[i])
p = 0 #プレイヤー
while True:
    if not S[0] and p == 0:
        print("A")
        break
    if not S[1] and p == 1:
        print("B")
        break
    if not S[2] and p == 2:
        print("C")
        break
    p_t = S[p].pop(0)
    if p_t == "a":
        p = 0
    elif p_t == "b":
        p = 1
    elif p_t == "c":
        p = 2