Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline().rstrip())
    d = {}
    for i in range(N):
        poll = stdin.readline().rstrip()
        if not poll in d:
            d[poll] = 1
        else:
            d[poll] += 1
    max_k_list = [kv[0] for kv in d.items() if kv[1] == max(d.values())]
    for key in sorted(max_k_list):
        print(key)

### FIXED CODE ###

from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline().rstrip())
    d = {}
    for i in range(N):
        poll = stdin.readline().rstrip()
        if not poll in d:
            d[poll] = 1
        else:
            d[poll] += 1
    maxv = max(d.values())
    max_k_list = [kv[0] for kv in d.items() if kv[1] == maxv]
    for key in sorted(max_k_list):
        print(key)
### BUGGY CODE ###

from collections import deque

n=int(input())
S=deque()

for i in range(n):
	cnt=-1
	str=input().split()
	if str[0][0]=="i":
		S.append(str[1])
	else:
		'''for i in range(len(S)):
			if S[i]==str[1]:
				cnt+=1
				break
		'''
		try:
			cnt=S.index(str[1])
		except:
			pass
		if cnt!=-1:
			print("yes")
		else:
			print("no")



### FIXED CODE ###

n=int(input())
dict={}
for i in range(n):
    str=input()
    if str[0]=="i":
        dict.update({str[7:]:1})
    else:
        if str[5:] in dict:
            print("yes")
        else:
            print("no")
### BUGGY CODE ###

# https://atcoder.jp/contests/abc141/tasks/abc141_e
# https://www.slideshare.net/hcpc_hokudai/ss-121539419
# これのほうがいいかな？ https://sen-comp.hatenablog.com/entry/2020/01/16/174230

# 結局これが一番わかりやすい https://qiita.com/Pro_ktmr/items/16904c9570aa0953bf05

# zアルゴリズムをやるらしい


def z_algorithm(S: str):
    '''SとS[i]の最長共通接頭辞長 Z[i] の配列を返す O(len(S))'''
    Z = [-1] * len(S)  # -1で初期化 (-1が出てきたらおかしい！)
    Z[0] = len(S)
    i = 1  # 現在求めたいZ[i]の位置
    j = 0  # S[i:i+j]の文字列は先頭と一致している
    while i < len(S):
        while (i + j < len(S) and S[j] == S[i + j]):
            j += 1
        Z[i] = j

        if j == 0:
            i += 1
            continue
        k = 1
        while (k < j and k + Z[k] < j):  # ならば再利用可能
            # ∵kからZ[k]後がjより小さいということは同一文字列の終了がjを突き抜けないことが保証されている
            # →Z[k]のほうが必ず早く終了するので値はZ[i+k]の値はZ[k]になる
            Z[i + k] = Z[k]
            k += 1
        i += k  # 次のiはまだ埋まっていないところまで移動
        j -= k  # jも無駄な探索を避けるために移動分だけの幅減少に留める
        # jの働き後半がはみ出すときは確定させないで次の探索で確定できるようにするというだけ
    return Z


N = int(input())
S = input()
# まずzアルゴリズムを任意の開始地点に用いる
# ほしいデータ構造
# S[i:]とS[j:]の最長共通接頭辞長行列 M[i][j]
# これのうち重複していないなかでの最大値が答え。
# つまりmax{M[i][j] | i+M[i][j]<=j}

# i,jはあくまで相対位置なのでMを実際に構成しなくてもiを一つずつずらしてjを調べるということでおk
ans = 0
for i in range(N):  # 開始位置
    Z = z_algorithm(S[i:])
    for j in range(N - i):
        if Z[j] <= j:
            ans = max(ans, Z[j])
print(ans)


### FIXED CODE ###

# https://atcoder.jp/contests/abc141/tasks/abc141_e
# https://www.slideshare.net/hcpc_hokudai/ss-121539419
# これのほうがいいかな？ https://sen-comp.hatenablog.com/entry/2020/01/16/174230

# 結局これが一番わかりやすい https://qiita.com/Pro_ktmr/items/16904c9570aa0953bf05

# zアルゴリズムをやるらしい


def z_algorithm(S: str):
    '''SとS[i]の最長共通接頭辞長 Z[i] の配列を返す O(len(S))'''
    Z = [-1] * len(S)  # -1で初期化 (-1が出てきたらおかしい！)
    Z[0] = len(S)
    i = 1  # 現在求めたいZ[i]の位置
    j = 0  # S[i:i+j]の文字列は先頭と一致している
    while i < len(S):
        while (i + j < len(S) and S[j] == S[i + j]):
            j += 1
        Z[i] = j

        if j == 0:
            i += 1
            continue
        k = 1
        while (k < j and k + Z[k] < j):  # ならば再利用可能
            # ∵kからZ[k]後がjより小さいということは同一文字列の終了がjを突き抜けないことが保証されている
            # →Z[k]のほうが必ず早く終了するので値はZ[i+k]の値はZ[k]になる
            Z[i + k] = Z[k]
            k += 1
        i += k  # 次のiはまだ埋まっていないところまで移動
        j -= k  # jも無駄な探索を避けるために移動分だけの幅減少に留める
        # jの働き後半がはみ出すときは確定させないで次の探索で確定できるようにするというだけ
    return Z


N = int(input())
S = input()
# まずzアルゴリズムを任意の開始地点に用いる
# ほしいデータ構造
# S[i:]とS[j:]の最長共通接頭辞長行列 M[i][j]
# これのうち重複していないなかでの最大値が答え。
# つまりmax{M[i][j] | i+M[i][j]<=j}

# i,jはあくまで相対位置なのでMを実際に構成しなくてもiを一つずつずらしてjを調べるということでおk
ans = 0
for i in range(N):  # 開始位置
    Z = z_algorithm(S[i:])
    for j in range(N - i):
        if Z[j] <= j:
            ans = max(ans, Z[j])
print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))

comb1 = max(a)
comb2 = comb1//2
a.sort()
p = 0
keep = float("inf")
for i in range(n):
    if abs(a[i] - comb2) <= keep:
        p = a[i]
        keep = abs(a[i]-comb2)
import math
ans = math.factorial(comb1)//((math.factorial(p))*(math.factorial(comb1-p)))
if comb1 == p:
    print(comb1,0)
else:
    print(comb1,p)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))

comb1 = max(a)
comb2 = comb1//2
a.sort()
p = 0
keep = float("inf")
for i in range(n):
    if abs(a[i] - comb2) <= keep:
        p = a[i]
        keep = abs(a[i]-comb2)

if comb1 == p:
    print(comb1,0)
else:
    print(comb1,p)
### BUGGY CODE ###

#!/mnt/c/Users/moiki/bash/env/bin/python
H,W,D = map(int, input().split())
m = [ list(map(int, input().split())) for _ in range(H)]
Q = int(input())
pos = {}

for h in range(H):
    for w in range(W):
        pos[m[h][w]] = (h,w)
        



d = {}


for i in range(1, 1+D):
    bhop = i
    hop = i + D
    while hop <= H*W:
        d[hop] = abs( pos[hop][0] - pos[bhop][0]) + abs( pos[hop][1] - pos[bhop][1]) 
        bhop = hop
        hop  = hop+D


for i in range(Q):
    L,R = map(int, input().split())
    ans = 0
    hop = L+D
    bhop = L
    while hop <= R:
        ans += d[hop]
        bhop = hop
        hop  = hop+D

    print(ans)
    


### FIXED CODE ###

#!/mnt/c/Users/moiki/bash/env/bin/python
H,W,D = map(int, input().split())
M = [ list(map(int, input().split())) for _ in range(H)]
Q = int(input())
pos = {}
from itertools import accumulate

for h in range(H):
    for w in range(W):
        pos[M[h][w]] = (h,w)
        

accd = [ [0] for _ in range(D)]

distance = lambda p,np: abs(p[0]-np[0])+abs(p[1]-np[1])

for i in range(1,D+1):
    p = pos[i]
    amari = i % D

    tmp = i + D

    while tmp <= H*W:
        nextp = pos[tmp]
        accd[amari].append( accd[amari][-1] + distance(p,nextp) )

        tmp += D
        p = nextp

# print(accd)
# exit(0)

for i in range(Q):
    L,R = map(int, input().split())
    ans = 0

    if not L == R:
        if R%D == 0:
            ans = accd[L%D][(R-R%D)//D-1] - accd[L%D][(L-L%D)//D-1]
        else:
            ans = accd[L%D][(R-R%D)//D] - accd[L%D][(L-L%D)//D]


    print(ans)
### BUGGY CODE ###

""" AtCoder """

N, Q = map(int, input().split())
S = input()
lr = [list(map(int, input().split())) for _ in range(Q)]

l = []
pos = 0

for _ in range(S.count("AC")):
    i = S.index("AC")
    l.append(i + pos + 1)
    pos = pos + i + 2
    S = S[i + 2:]

for i in range(Q):
    tmp = []
    tmp.extend(l)
    tmp.append(lr[i][0])
    tmp.append(lr[i][1])
    tmp.sort()
    print(tmp.index(lr[i][1]) - tmp.index(lr[i][0]) - 1)


### FIXED CODE ###

""" AtCoder """

N, Q = map(int, input().split())
S = input()
lr = [list(map(int, input().split())) for _ in range(Q)]

tmp = 0
l = [0]

for i in range(1, N):
    if S[i - 1] + S[i] == "AC":
        tmp += 1
    l.append(tmp)

for i in range(Q):
    print(l[lr[i][1]-1] - l[lr[i][0]-1])
### BUGGY CODE ###

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

for i in range(M):
    B, C = map(int, input().split())
    for j in range(B):
        if A[j] < C:
            A[j] = C
        else:
            break
    A.sort()

ans = sum(A)
print(ans)


### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
p = [[A[i], 1] for i in range(N)]   # Aのカードは各1枚ずつ

for i in range(M):
    B, C = map(int, input().split())
    p.append([C, B])    # CのカードをB枚追加

p.sort()
p.reverse()

ans = 0
cnt = N
for v, n in p:
    num = min(cnt, n)    # 残り枚数とvのカード枚数の少ない方を採用
    ans += v * num
    cnt -= num
    if cnt == 0:
        break

print(ans)
### BUGGY CODE ###

N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp)
day = 0
game = [0]*N
while rem:
    day += 1
    impossible = True
    played = [False]*N
    for i in range(N):
        if played[i] or game[i] >= N-1: continue
        opposite = A[i][game[i]] - 1
        if not played[opposite] and i+1 == A[opposite][game[opposite]]:
            impossible = False
            played[i] = True; played[opposite] = True;
            rem -= 1
            game[i] += 1; game[opposite] += 1
    if impossible: print(-1); break
else:
    print(day)

### FIXED CODE ###

N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp[::-1])
day = 0
next_opponent = [-1]*N
played_yesterday = list(range(N))
while rem:
    day += 1
    impossible = True
    for p in played_yesterday:
        if A[p]: next_opponent[p] = A[p].pop() - 1
    played_today = []
    for p in played_yesterday:
        candidate = next_opponent[p]
        if candidate == -1: continue # player p had already finished all the game
        elif next_opponent[candidate] == p: # matched
            impossible = False
            rem -= 1
            played_today += [p, candidate]
            next_opponent[p], next_opponent[candidate] = -1, -1
    if impossible: print(-1); break
    played_yesterday = played_today
else:
    print(day)
### BUGGY CODE ###

N = int(input())

dic = [[] for i in range(N)]
for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  dic[u-1] +=[(v-1, w)]
  dic[v-1] +=[(u-1, w)]

dist = [-1]*N
stack = []
stack.append(0)
dist[0] = 0

while stack:
  label = stack.pop(-1)
  for i, c in dic[label]:
    if dist[i] == -1:
      dist[i] = dist[label]+c
      stack += [i]

ls = [0]
print(0)
for i in range(1,N):
  j = i-1
  for j in range(i):
    if (dist[j]+dist[i])%2==1:
      print((ls[j]+1)%2)
      ls += [(ls[j]+1)%2]
      break
  else:
    print(ls[i-1])
    ls += [ls[i-1]]



### FIXED CODE ###

N = int(input())

dic = [[] for i in range(N)]
for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  dic[u-1] +=[(v-1, w)]
  dic[v-1] +=[(u-1, w)]

dist = [-1]*N
stack = []
stack.append(0)
dist[0] = 0

while stack:
  label = stack.pop(-1)
  for i, c in dic[label]:
    if dist[i] == -1:
      dist[i] = dist[label]+c
      stack += [i]

ls = [0]
print(0)
for i in range(1,N):
  if dist[i]%2==0:
    print(0)
  else:
    print(1)
### BUGGY CODE ###

bingo = []

for i in range(3):
    bingo.append(list(map(int, input().split())))

flat = [flatten for inner in bingo for flatten in inner]

n = int(input())

nums = []

for i in range(n):
    nums.append(int(input()))

for i in nums:
    if i in flat:
        flat[flat.index(i)] = 0

bingo = [flat[:3], flat[3:6], flat[6:]]
bingo_z = [x for x in zip(*bingo)]

count = 0
while count == 0:
    for i in range(3):
        if sum(bingo[i]) == 0:
            count += 1
            break

    for i in range(3):
        if sum(bingo_z[i]) == 0:
            count += 1
            break

    if (bingo[0][0] + bingo[1][1] + bingo[2][2] == 0) or (bingo[2][0] + bingo[1][1] + bingo[0][2] == 0):
        count += 1
        break

print("No" if count == 0 else "Yes")


### FIXED CODE ###

bingo = [input().split() for _ in range(3)]

n = int(input())

nums = [input() for _ in range(n)]

check = [[] for _ in range(3)]

for i in range(3):
    for j in range(3):
        if bingo[i][j] in nums:
            check[i].append(True)
        else:
            check[i].append(False)

ANS = "No"

for i in range(3):
    if (check[i][0]) and (check[i][1]) and (check[i][2]):
        ANS = "Yes"
for i in range(3):
    if (check[0][i]) and (check[1][i]) and (check[2][i]):
        ANS = "Yes"
if (check[0][0]) and (check[1][1]) and (check[2][2]):
    ANS = "Yes"
if (check[0][2]) and (check[1][1]) and (check[2][0]):
    ANS = "Yes"

print(ANS)
### BUGGY CODE ###

def topological_sort(V, E_in, E_from):
    '''
    V:N(node)
    E_from:
    E_in:
    '''

    # count num of pointed nodes for each nodes
    cnt = [0]*V
    Q = []
    for v in range(V):
        cnt[v] = len(E_in[v])
        if cnt[v] == 0:
            Q.append(v)

    ret = []
    while Q:
        del_node = Q.pop()
        ret.append(del_node)
        for j in E_from[del_node]:
            cnt[j] -= 1
            if cnt[j] == 0:
                Q.append(j)

    return ret


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

    top_sort = topological_sort(N, E_in, E_from)
    parent = [0]*N
    for i in range(N):
        current = top_sort[i]
        for j in reversed(top_sort[:i]):
            if current in E_from[j]:
                parent[current] = j+1
                break

    ans = '\n'.join(map(str, parent))
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def topological_sort(V, E_in, E_from):
    '''
    V:N(node)
    E_from:
    E_in:
    '''

    # count num of pointed nodes for each nodes
    parent = [0]*V
    cnt = [0]*V
    Q = []
    for v in range(V):
        cnt[v] = len(E_in[v])
        if cnt[v] == 0:
            Q.append(v)

    while Q:
        del_node = Q.pop()
        for j in E_from[del_node]:
            cnt[j] -= 1
            if cnt[j] == 0:
                Q.append(j)
                parent[j] = del_node+1

    return parent


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

    ans = topological_sort(N, E_in, E_from)
    for s in ans:
        print(s)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

from collections import Counter

N = int(input())
input_list = []
input_counter = Counter()
ans = 10**10
index = 1
for i in input().split():
    item = int(i) - index
    input_list.append(item)
    input_counter[item] += 1
    index += 1


for i in input_counter.keys():
    ans = min(ans, sum(map(lambda x: abs(x-i), input_list)))

print(ans)

### FIXED CODE ###

N = int(input())

input_list = []
index = 1
for i in input().split():
    item = int(i) - index
    input_list.append(item)
    index += 1

input_list.sort()
b = input_list[N//2]

print(sum(map(lambda x: abs(x-b), input_list)))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
i = 0
while i < N:
    num = 0
    for j in A:
        if j == i + 1:
            num += 1
    i += 1
    print(num)    



### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = [0] * N
for i in range(N - 1):
    ans[A[i] - 1] += 1
for i in range(N):
    print(ans[i])
### BUGGY CODE ###

import heapq as hq
N, K = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
F = list(map(int,input().split()))
F.sort()

if sum(A) <= K:
	print(0)
	exit()
ans = max([A[i]*F[N-1-i] for i in range(N)])

def achievable(score):
	cnt = 0
	for i in range(N):
		a,f = A[i], F[N-1-i]
		if a*f > score:
			n = score // f
			cnt += a-n
			if cnt > K:
				return False
	return True

l,r = 0, ans
while r - l > 1:
	p = (l + r) // 2
	if achievable(p):
		r = p
	else:
		l = p
print(r)

### FIXED CODE ###

import heapq as hq
N, K = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
F = list(map(int,input().split()))
F.sort()

if sum(A) <= K:
	print(0)
	exit()
ans = max([A[i]*F[N-1-i] for i in range(N)])

def achievable(score):
	cnt = 0
	for i in range(N):
		a,f = A[i], F[N-1-i]
		if a*f > score:
			n = score // f
			cnt += a-n
			if cnt > K:
				return False
	return True

l,r = 0, ans
while r - l > 1:
	p = (l + r) // 2
	if achievable(p):
		r = p
	else:
		l = p
print(r)
### BUGGY CODE ###

n=int(input())
num=[list(map(int,input().split())) for _ in range(n)]
n1=[]
num1=10**9
ans=0
for i in range(n):
    n1.append(num[i][0]+num[i][1])
    ans=max(ans,max(n1)-min(n1))
n1=[]
for i in range(n):
    n1.append(num1-num[i][0]+num[i][1])
    ans=max(ans,max(n1)-min(n1))
print(ans)


### FIXED CODE ###

n=int(input())
num=[list(map(int,input().split())) for _ in range(n)]
n1=[]
num1=10**9
ans=0
for i in range(n):
    n1.append(num[i][0]+num[i][1])
ans=max(ans,max(n1)-min(n1))
n1=[]
for i in range(n):
    n1.append(num1-num[i][0]+num[i][1])
ans=max(ans,max(n1)-min(n1))
print(ans)
### BUGGY CODE ###

#!/usr/local/bin/python3
# coding: utf-8
n = input()
hairu = input()
kita = hairu.split(" ")
a = []
seki = 1
for c in kita:
    a.append(c)

try:
    for c in a:
        seki = seki * int(c)
    if seki > 10**18:
        print("-1")
    else:
        print(seki)
except:
    print("-1")

### FIXED CODE ###

#!/usr/local/bin/python3
# coding: utf-8
n = input()
hairu = input()
kita = hairu.split(" ")
seki = 1

for c in kita:
    if c == "0":
        kita[0] = "0"

for c in kita:
    seki = seki * int(c)
    if seki > 10**18:
        break

if seki > 10**18:
    print("-1")
else:
    print(seki)
### BUGGY CODE ###

import collections as co
N,M =list(map(int,input().split()))
card=[]
ans=0
for i in range(M):
  st,en=list(map(int,input().split()))
  for cn in range(st,en+1):
    card.append(cn)
vv=co.Counter(card)
for k in range(N+1):
  if M==vv[k]:
    ans+=1
print(ans)


### FIXED CODE ###

N,M =list(map(int,input().split()))
la=1
rm=N
for i in range(M):
  a,b=list(map(int,input().split()))
  la=max(la,a)
  rm=min(rm,b)
print(max(0,rm-la+1))
### BUGGY CODE ###

n = int(input())
xx = list(map(int, input().split()))

for i in range(n):
  yy = xx[:i] + xx[i+1:]
  zz = sorted(yy)
  print(zz[(n-1)//2])

### FIXED CODE ###

n = int(input())
xx = list(map(int, input().split()))
yy = sorted(xx)

a = yy[n//2-1]
b = yy[n//2]

for x in xx:
  if x <= a:
    print(b)
  elif x >= b:
    print(a)
### BUGGY CODE ###

s=input()
c=0
while "ABC" in s:
    s=s.replace("ABC","BCA",1)
    c+=1
print(c)

### FIXED CODE ###

import re
s=re.split("B|C",input().replace("BC","D"))
c,cd=0,0
for i in s:
    for j in range(len(i)):
        if i[j]=="D":
            c+=j-cd
            cd+=1
    cd=0
print(c)
### BUGGY CODE ###

MOD = 10 ** 9 + 7

def solve():
    N, M = map(int, input().split())
    A = [int(input()) for _ in range(M)]
    
    if M > 1:
        for i in range(M-1):
            if A[i]+1 == A[i+1]:
                return 0

    if N == 1 and M == 0:
        return 1
    elif N == 1 and M == 1 and A[0] == 0:
        return 0

    dp = [0] * (N+1)
    dp[1] = 0 if 1 in A else 1
    dp[2] = 0 if 2 in A else dp[1] + 1

    for i in range(3, N+1):
        if i in A:
            dp[i] = 0
        else:
            dp[i] = dp[i-1] + dp[i-2]

    return dp[N] % MOD

print(solve())

### FIXED CODE ###

MOD = 10 ** 9 + 7

def solve():
    N, M = map(int, input().split())
    A = [int(input()) for _ in range(M)]
    
    if N == 1 and M == 0:
        return 1
    elif N == 1 and M == 1 and A[0] == 0:
        return 0

    l = [1 for _ in range(N+1)]
    for a in A:
        l[a] = 0
        
    dp = [0 for i in range(N+1)]
    dp[1] = 0 if 1 in A else 1
    dp[2] = 0 if 2 in A else dp[1] + 1

    for i in range(3, N+1):
        if l[i] == 0:
            dp[i] = 0
        else:
            dp[i] = dp[i-1] + dp[i-2]
            dp[i] %= MOD

    return dp[N] % MOD

print(solve())
### BUGGY CODE ###

import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]

    start, goal = [], []
    for h in range(H):
        for w in range(W):
            if S[h][w] != "#":
                start.append([h, w])
                goal.append([h, w])
                
    res = 0
    for sh, sw in start:
        for gh, gw in goal:
            maze = [[f_inf] * W for _ in range(H)]
            maze[sh][sw] = 0
            que = deque([[sh, sw]])
            while que:
                h, w = que.popleft()
                for dh, dw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                    next_h, next_w = h + dh, w + dw
                    if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                        continue
                    elif S[next_h][next_w] == "#":
                        continue
                    else:
                        if maze[next_h][next_w] > maze[h][w] + 1:
                            maze[next_h][next_w] = maze[h][w] + 1
                            que.append([next_h, next_w])
            if maze[gh][gw] != f_inf:
                res = max(res, maze[gh][gw])
    print(res)


if __name__ == '__main__':
    resolve()


### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
grid = [input() for _ in range(H)]

f_inf = float('inf')
ans = []
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            continue
        else:
            maze = [[f_inf] * W for _ in range(H)]
            sh, sw = h, w
            maze[sh][sw] = 0
            que = deque([[sh, sw]])
            while que:
                h, w = que.popleft()
                for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                    next_h, next_w = h + dh, w + dw
                    if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                        continue
                    elif grid[next_h][next_w] == "#":
                        continue
                    elif grid[next_h][next_w] == ".":
                        if maze[next_h][next_w] > maze[h][w] + 1:
                            maze[next_h][next_w] = maze[h][w] + 1
                            que.append([next_h, next_w])
                            ans.append(maze[next_h][next_w])

print(max(ans))
### BUGGY CODE ###

N = int(input())
D = []
ans = 0

for _ in range(N):
    S = input()
    if S not in D:
        D.append(S)
        ans += 1

print(ans)


### FIXED CODE ###

N = int(input())
D = {}
ans = 0

for _ in range(N):
    S = input()
    if S not in D:
        D[S] = True
        ans += 1

print(ans)
### BUGGY CODE ###

N, M = map(int,input().split())
P = []
Y = []
B = [[] for _ in range(N)]
for _ in range(M):
    p, y = map(int,input().split())
    P.append(p)
    Y.append(y)
    B[p-1].append(y)

for i in range(N):
    B[i].sort()

for p, y in zip(P, Y):
    print("{:06}{:06}".format(p, B[p-1].index(y) + 1))






### FIXED CODE ###

import bisect
N, M = map(int,input().split())
P = []
Y = []
B = [[] for _ in range(N)]
for _ in range(M):
    p, y = map(int,input().split())
    P.append(p)
    Y.append(y)
    B[p-1].append(y)

for i in range(N):
    B[i].sort()

for p, y in zip(P, Y):
    print("{:06}{:06}".format(p, bisect.bisect_left(B[p-1], y) + 1))