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

def main():
    from collections import deque
    import sys
    # from time import time

    # start = time()

    H, W = map(int, sys.stdin.readline().split())
    field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

    # print(time() - start)

    queue = deque([])

    for h in range(H):
        for w in range(W):
            if field[h][w] == "#":
                queue.append([h, w])

    dist_from_nearest_black = [[0] * W for i in range(H)]
    direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    def bfs():
        while len(queue) != 0:

            h, w = queue.popleft()

            for dh, dw in direction:
                next_h = h + dh
                next_w = w + dw

                if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                    continue
                elif field[next_h][next_w] == "#":
                    continue
                else:
                    dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                    field[next_h][next_w] = "#"
                    queue.append([next_h, next_w])

    bfs()

    ans = 0

    for d in dist_from_nearest_black:
        ans = max(ans, max(d))

    print(ans)
    # print(time() - start)


if __name__ == "__main__":
    main()


### FIXED CODE ###

from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)
### BUGGY CODE ###

R, G, B, N = map(int, input().split())

r, g, b = int(N // R), int(N // G), int(N // B)

ans = 0
for i in range(r + 1):
    if i * R > N:
        break
    for j in range(g + 1):
        if (N - (i * R + j * G)) % B == 0 and (N - (i * R + j * G)) >= 0:
            ans += 1
print(ans)


### FIXED CODE ###

R, G, B, N = map(int, input().split())

r, g, b = int(N // R), int(N // G), int(N // B)

ans = 0
for i in range(r + 1):
    if i * R > N:
        break
    for j in range(g + 1):
        if (N - (i * R + j * G)) % B == 0 and (N - (i * R + j * G)) >= 0:
            ans += 1
print(ans)
### BUGGY CODE ###

import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, Q = il()
    S = iss()
    ret = [0] * N
    for n in range(N - 1):
        if S[n] == 'A' and S[n + 1] == 'C':
            ret[n] += 1

    for _ in range(Q):
        l, r = il()
        print(sum(ret[l - 1:r])-sum(ret[r-1:r]))


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, Q = il()
    S = iss()
    ret = [0] * N
    for n in range(N - 1):
        if S[n] == 'A' and S[n + 1] == 'C':
            ret[n] += ret[n - 1] + 1
        else:
            ret[n] += ret[n - 1]

    for _ in range(Q):
        l, r = il()
        print(ret[r - 2] - ret[l - 2])


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

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

ans = 0
for x in range(0,K+1):
    for y in range(0,K+1):
        if x + y > S:
            break
        for z in range(0,K+1):
            if x + y + z == S:
                ans += 1
            if x + y + z > S:
                break

print(ans)


### FIXED CODE ###

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

ans = 0
for x in range(0,K+1):
    for y in range(0,K+1):
        z = S - x - y
        if 0 <= z <= K:
            ans += 1

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

def c_stones(N, S):
    import itertools
    if S.count('#') == 0 or S.count('.') == 0:
        return 0
    s = [(k, len(list(v))) for k, v in itertools.groupby(S)]
    ans = float('inf')
    for i in range(len(s)):
        ans1, ans2 = 0, 0
        if s[i][0] == '#':
            # ここから黒にする
            for j in range(len(s)):
                if j < i and s[j][0] == '#':
                    ans1 += s[j][1]
                elif j > i and s[j][0] == '.':
                    ans1 += s[j][1]
        elif s[i][0] == '.':
            # ここまで白にする
            for j in range(len(s)):
                if j < i and s[j][0] == '#':
                    ans1 += s[j][1]
                elif j > i and s[j][0] == '.':
                    ans1 += s[j][1]
        ans = min(ans1, ans)
    return ans

N = int(input())
S = input()
print(c_stones(N, S))


### FIXED CODE ###

def c_stones(N, S):
    black_left_of_the_boundary, white_right_of_the_boundary = 0, S.count('.')
    ans = black_left_of_the_boundary + white_right_of_the_boundary
    for c in S:
        if c == '#':
            black_left_of_the_boundary += 1
        else:
            white_right_of_the_boundary -= 1
        ans = min(ans, black_left_of_the_boundary + white_right_of_the_boundary)
    return ans

N = int(input())
S = input()
print(c_stones(N, S))
### BUGGY CODE ###

N,K = map(int,input().split())
p_list = list(map(int,input().split()))
ans_list = []
def E(me):
    if me == 1:
        return 1
    else:
        return ((1+me)*me/2)/me
E_list = [E(i) for i in p_list]
for i in range(N-K+1):
    ans_list.append(sum(E_list[i:i+K]))
print(max(ans_list))


### FIXED CODE ###

N,K = map(int,input().split())
p_list = list(map(int,input().split()))
ans_list = []
def E(me):
    if me == 1:
        return 1
    else:
        return ((1+me)*me/2)/me
E_list_ruisekiwa = [0,]
for i in range(N):
    E_list_ruisekiwa.append(E_list_ruisekiwa[i]+E(p_list[i]))

ans = 0
for i in range(N-K+1):
    ans = max(E_list_ruisekiwa[i+K]-E_list_ruisekiwa[i],ans)
print(ans)
### BUGGY CODE ###

l = raw_input().split()
N = int(l[0])
M = int(l[1])

BC = []

l = raw_input().split()
A = [int(x) for x in l]

for i in range(M):
    l = raw_input().split()
    bc = [int(l[0]), int(l[1])]
    BC.append(bc)

A.sort()
BC.sort(key = lambda x: x[1])
# print(N, M, A, BC)

for i in range(M):
    # print(BC[i])
    count = 0
    index = 0
    b = BC[i][0]
    c = BC[i][1]
    for j in range(N):
        if A[j] >= c:
            break
        index += 1
        if j < b:
            count += 1
    # print(index, count, A)
    for j in range(count):
        A.insert(index, c)
    del A[:count]
    # print(A)

# print(A)
print(sum(A))


### FIXED CODE ###

l = raw_input().split()
N = int(l[0])
M = int(l[1])

BC = []

l = raw_input().split()
A = [int(x) for x in l]

for i in range(M):
    l = raw_input().split()
    bc = [int(l[0]), int(l[1])]
    BC.append(bc)

A.sort()
BC.sort(key = lambda x: -x[1])
# print(N, M, A, BC)

i0 = 0
i1 = 0
for i in range(N):
    # print(BC[i0])
    b = BC[i0][0]
    c = BC[i0][1]

    if A[i] < c:
        A[i] = c
        i1 += 1
    if i1 == b:
        i0 += 1
        i1 = 0
    # print(A)
    if i0 == M:
        break

# print(A)
print(sum(A))
### BUGGY CODE ###

#3問目
NM = input().split()
N,M = int(NM[0]), int(NM[1])
A = []
dp = [0] * (N+1)
dp[0] = 1
dp[1] = 1
MOD = 1000000007
for _ in range(M):
    A.append(int(input()))
    
if(1 in A):
    dp[1] = 0

for i in range(2, N+1):
    #貰うdp
    if(i in A):
        dp[i] = 0
    else:
        dp[i] = dp[i-1] + dp[i-2]
    dp[i] %=MOD
    
print(dp[N])

### FIXED CODE ###

N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])
INF = 1000000007

dp = [0] * (N + 1)
dp[0] = 1
if 1 not in a:
	dp[1] = 1
else:
	dp[1] = 0
for i in range(2, N + 1):
	if i in a:
		continue
	dp[i] = dp[i - 1] + dp[i - 2]
	dp[i] %= INF

print(dp[N])
### BUGGY CODE ###

N, S = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353
dp = [[0]*(S+1) for _ in range(N+1)]
def main():
  dp[0][0] = 1
  for i in range(1, N+1):
    for j in range(0, S+1):
      dp[i][j] += (2*dp[i-1][j])%MOD
      dp[i][j] %= MOD
      if j+A[i-1] <= S:
        dp[i][j+A[i-1]] += dp[i-1][j]%MOD
        dp[i][j+A[i-1]] %= MOD
  print(dp[N][S])

if __name__ == "__main__":
  main()

### FIXED CODE ###

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

def main():
  dp = [0]*(S+1)
  dp[0] = 1
  for i in range(1, N+1):
    p = [0]*(S+1)
    dp, p = p, dp
    for j in range(0, S+1):
      dp[j] += 2*p[j]
      dp[j] %= MOD
      if j-A[i-1] >= 0:
        dp[j] += p[j-A[i-1]]
        dp[j] %= MOD
  print(dp[S])

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

n, k = map(int, input().split())
s = input()

# 100101 => [1,2,1,1,1]
# 011000 => [0,1,2,3,0]
human = []

is_handstood = False
cnt = 0

for ch in s:
    if ch == "0" and not is_handstood:
    	human.append(cnt)
    	is_handstood = True
    	cnt = 1
    elif ch == "1" and is_handstood:
    	human.append(cnt)
    	is_handstood = False
    	cnt = 1
    else:
        cnt += 1
human.append(cnt)
if is_handstood:
    human.append(0)

max_human = 0

if len(human) <= 2 * k + 1:
    max_human = n
else:
    for i in range(0, len(human) - 2 * k, 2):
        max_human = max(sum(human[i:i+2*k+1]), max_human)

print(max_human)

### FIXED CODE ###

n, k = map(int, input().split())
s = input()

# 100101 => [1,2,1,1,1]
# 011000 => [0,1,2,3,0]
human = []

is_handstood = False
cnt = 0

for ch in s:
    if ch == "0" and not is_handstood:
    	human.append(cnt)
    	is_handstood = True
    	cnt = 1
    elif ch == "1" and is_handstood:
    	human.append(cnt)
    	is_handstood = False
    	cnt = 1
    else:
        cnt += 1
human.append(cnt)
if is_handstood:
    human.append(0)

total_human = [0 for i in range(len(human))]
total_human[0] = human[0]
for i in range(1, len(human)):
    total_human[i] = total_human[i - 1] + human[i]

if len(human) <= 2 * k + 1:
    max_human = n
else:
    max_human = total_human[k * 2]
    for i in range(k * 2 + 2, len(total_human), 2):
        if total_human[i] - total_human[i - k * 2 - 1] > max_human:
        	max_human = total_human[i] - total_human[i - k * 2 - 1]

print(max_human)
### BUGGY CODE ###

# review series
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
A.sort()
divisible = [False for i in range(10**6+1)]
for x in A:
    cnt = 2*x
    while cnt <= 10**6:
        divisible[cnt] = True
        cnt += x
D = dict(Counter(A))
ans = 0
for key, value in D.items():
    if D[key] == 1 and divisible[key] == False:
        ans += 1
print(ans)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
visited=[False for i in range(10**6+1)]
ban=[False for i in range(10**6+1)]
from collections import Counter
A.sort()
D=dict(Counter(A))
for n in A:
    if visited[n]==False:
        visited[n]=True
        for j in range(2,10**6):
            if 10**6<n*j:
                break 
            ban[n*j]=True
ans=0
for n in A:
    if D[n]==1 and ban[n]==False:
        ans+=1
print(ans)
### BUGGY CODE ###

# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, A, B, K = lr()
answer = 0
# A * x + B * y = K
# nCx * nCy



def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = N + 100 # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)

for x in range(N+1):
    temp = K-A*x
    if temp%B != 0:
        continue
    y = temp // B
    if y > N:
        continue
    if y < 0:
        break
    answer += cmb(N, x) * cmb(N, y)
    answer %= MOD

print(answer % MOD)


### FIXED CODE ###

# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, A, B, K = lr()
answer = 0
# A * x + B * y = K
# nCx * nCy

def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact_N * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = N + 100 # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)
fact_N = fact[N]

for x in range(N+1):
    temp = K-A*x
    if temp%B != 0:
        continue
    y = temp // B
    if y > N:
        continue
    if y < 0:
        break
    answer += cmb(N, x) * cmb(N, y)
    answer %= MOD

print(answer % MOD)
### BUGGY CODE ###

def F(a, b):
    count = 0
    c = max(a, b)
    while c > 9:
        c = c / 10
        count += 1
    return count+1

N = int(input())
minimum = 11
for num in range(1,int(N/4)+5):
    if N % num == 0:
        minimum = min(minimum, F(num, N/num))
print(minimum)

### FIXED CODE ###

def F(a, b):
    count = 0
    c = max(a, b)
    while c >= 10:
        c = c / 10
        count += 1
    return count+1

N = int(input())
minimum = 15
for num in range(1,int(N/(10**5))+10**5+1):
    if N % num == 0:
        minimum = min(minimum, F(num, N/num))
print(minimum)
### BUGGY CODE ###

s=input()
t=input()
l=len(s)
count=0
for i in range(l):
  if(s[i]==t[i]):
    continue
  else:
    s.replace(s[i],t[i])
    count+=1

print(count)

### FIXED CODE ###

s=input()
t=input()
l=len(s)
count=0
if(s==t):
  print(count)
  
else:
  for i in range(l):
    if(s[i]==t[i]):
      continue
    else:
      count+=1

  print(count)
### BUGGY CODE ###

import heapq as q

n, k = map(int, input().split())
l = []
q.heapify(l)

for i in range(n):
    a, b = map(int, input().split())
    for j in range(b):
        q.heappush(l, a)

for i in range(k):
    ans = q.heappop(l)
print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
l = [0]*(10**5+10)

for i in range(n):
    a, b = map(int, input().split())
    l[a] += b

sum = 0
for i in range(len(l)):
    sum += l[i]
    if sum >= k:
        print(i)
        break
### BUGGY CODE ###

from collections import defaultdict
inf = float('inf')

def solve(graph, n, start_node, end_node):
  costs = [inf for _ in range(n)]
  costs[start_node] = 0
  flgs = [False for _ in range(n)]

  while not all(flgs):
    prv_node = _get_min_idx(flgs,costs)
    flgs[prv_node] = True

    for node, dis in graph[prv_node]:
      if costs[node] > costs[prv_node] + dis:
        costs[node] = costs[prv_node] + dis
  return costs[end_node]
    
    
def _get_min_idx(flgs, costs):
    min_cost = inf
    min_idx = None
    cnt = 0
    for cost, flg in zip(costs, flgs):
      if min_cost > cost and flg is False:
        min_cost = cost
        min_idx = cnt
      cnt += 1
    return min_idx
        

n =int(input())
graph = defaultdict(list)

for _ in range(n-1):
  a,b,c = map(int, input().split())
  graph[a-1].append([b-1,c])
  graph[b-1].append([a-1,c])

q, k = map(int, input().split())
k -= 1
for _ in range(q):
  x, y = map(int, input().split())
  print( solve(graph, n, x-1, k) + solve(graph, n, k, y-1))

### FIXED CODE ###

from collections import defaultdict, deque
inf = float('inf')

def solve(graph, n, start_node, end_node):
  if start_node == end_node:
    return 0

  global costs_dict
  key = str(start_node) + '-' + str(end_node)
  if key in costs_dict:
    return costs_dict[key][end_node]

  costs = [inf for _ in range(n)]
  costs[start_node] = 0
  flgs = [False for _ in range(n)]

  while not all(flgs):
    prv_node = _get_min_idx(flgs,costs)
    flgs[prv_node] = True

    for node, dis in graph[prv_node]:
      if costs[node] > costs[prv_node] + dis:
        costs[node] = costs[prv_node] + dis
  costs_dict[key] = costs  
  return costs[end_node]
    
    
def _get_min_idx(flgs, costs):
    min_cost = inf
    min_idx = None
    cnt = 0
    for cost, flg in zip(costs, flgs):
      if min_cost > cost and flg is False:
        min_cost = cost
        min_idx = cnt
      cnt += 1
    return min_idx
        

n =int(input())
graph = defaultdict(list)
costs_dict = {}

for _ in range(n-1):
  a,b,c = map(int, input().split())
  graph[a-1].append([b-1,c])
  graph[b-1].append([a-1,c])

q, k = map(int, input().split())
k -= 1

l = [0 for _ in range(n)]
queue = deque()
queue.append(k)
while len(queue) > 0:
  a = queue.pop()
  for b, c in graph[a]:
    if l[b] == 0:
      l[b] = l[a] + c
      queue.append(b)

for _ in range(q):
  x, y = map(int, input().split())
  print( l[x-1] + l[y-1])
### BUGGY CODE ###

import sys
def main():
  input=sys.stdin.readline
  h,w=map(int,input().split())
  a=[list(map(int,input().split())) for _ in range(h)]
  b=[list(map(int,input().split())) for _ in range(h)]
  m=80*(h+w)
  dp=[[[False]*(m+1) for i in range(w)] for j in range(h)]
  dp[0][0][abs(a[0][0]-b[0][0])]=True
  for i in range(h):
    for j in range(w):
      for k in range(m+1):
        if dp[i][j][k]:
          if i<h-1:
            if abs(k+a[i+1][j]-b[i+1][j])<=m:
              dp[i+1][j][abs(k+a[i+1][j]-b[i+1][j])]=True
            if abs(k+b[i+1][j]-a[i+1][j])<=m:
              dp[i+1][j][abs(k+b[i+1][j]-a[i+1][j])]=True
          if j<w-1:
            if abs(k+a[i][j+1]-b[i][j+1])<=m:
              dp[i][j+1][abs(k+a[i][j+1]-b[i][j+1])]=True
            if abs(k+b[i][j+1]-a[i][j+1])<=m:
              dp[i][j+1][abs(k+b[i][j+1]-a[i][j+1])]=True
  for i in range(m+1):
    if dp[h-1][w-1][i]:
      return print(i)
if __name__=='__main__':
  main()


### FIXED CODE ###

import sys
def main():
  input=sys.stdin.readline
  h,w=map(int,input().split())
  a=[list(map(int,input().split())) for _ in range(h)]
  b=[list(map(int,input().split())) for _ in range(h)]
  m=80*162
  flg=(1<<(m*2))-1
  dp=[[0]*w for j in range(h)]
  dp[0][0]|=1<<(a[0][0]-b[0][0]+m)
  dp[0][0]|=1<<(b[0][0]-a[0][0]+m)
  for i in range(h):
    for j in range(w):
      if i<h-1:
        ne=abs(a[i+1][j]-b[i+1][j])
        dp[i+1][j]|=(dp[i][j]<<ne)&flg
        dp[i+1][j]|=(dp[i][j]>>ne)&flg
      if j<w-1:
        ne=abs(a[i][j+1]-b[i][j+1])
        dp[i][j+1]|=(dp[i][j]<<ne)&flg
        dp[i][j+1]|=(dp[i][j]>>ne)&flg
  ans=10**10
  for i in range(160*162):
    if (dp[h-1][w-1]>>i)&1:
      ans=min(ans,abs(i-m)) 
  return print(ans)
if __name__=='__main__':
  main()
### BUGGY CODE ###

n=int(input())
you=[]
al=[]
vot=[]
for i in range(n):
  s=input()
  al.append(s)
  if not s in you:
    you.append(s)
vot=[]
for i in you:
   vot.append(al.count(i)) 
inum = [n for n, v in enumerate(vot) if v==max(vot)]

ans=[]
for i in inum:
  ans.append(you[i])
ans.sort()
for i in ans:
  print(i)

### FIXED CODE ###

n=int(input())
you=[]
al=[]
vot=[]
ans=[]
for i in range(n):
  s=input()
  al.append(s)
al.sort()
bef="zzzzzzzzzzz"
co=0
mx=1
for x in al:
  if bef!=x:
    if mx<co:
      mx=co
      ans=[bef]
    elif mx==co:
      ans.append(bef)
    co=1
    bef=x
  else:
    co+=1
if mx<co:
  mx=co
  ans=[bef]
elif mx==co:
  ans.append(bef)
for x in ans:
  print(x)
### BUGGY CODE ###

from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
base = ["#" *(w+4)]
base.append("#" * (w+4))
for n in range(h):
    base.append("##" + input() + "##")
base.append("#" * (w+4))
base.append("#" * (w+4))
ans = [[-1] * (w+4) for _ in range(h+4)]
for j in range(w+4):
    for i in range(h+4):
        if base[i][j] == "#":
            ans[i][j] = -2
ch+=1; cw+=1
dh+=1; dw+=1
ans[ch][cw] = 0
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]
move2 = [
    [-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], \
    [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], \
    [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], \
    [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], \
    [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]]
q = deque()
one_time = deque()
q.append([ch, cw])
while q:
    x, y = q.popleft()
    one_time.append([x, y])
    for i, j in move:
        v1, v2 = x+i, y+j
        if ans[v1][v2] == -1:
            ans[v1][v2] = ans[x][y]
            q.append([v1, v2])
    if len(q) == 0:
        while one_time:
            x2, y2 = one_time.popleft()
            for i, j in move2:
                v1, v2 = x2+i, y2+j
                if ans[v1][v2] == -1:
                    ans[v1][v2] = ans[x2][y2] + 1
                    q.append([v1, v2])
print(ans[dh][dw])

### FIXED CODE ###

from collections import deque
 
 
# In[30]:
 
 
def main():
    H,W = map(int, input().split())
    ch,cw = map(int, input().split())
    dh,dw = map(int, input().split())
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [
        [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
        [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
        [0,-2],[0,-1],[0,0],[0,1],[0,2],\
        [1,-2],[1,-1],[1,0],[1,1],[1,2],\
        [2,-2],[2,-1],[2,0],[2,1],[2,2]
        ]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print(ans[dh][dw])
 
if __name__ == "__main__":
    main()