Harryxun/stf_run huggingface dataset:
n=int(input())
a=list(map(int, input().split()))
q=int(input())

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        print(ans)
        table[b]=0
        S=ans
    else:
        print(S)

### FIXED CODE ###

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

S=sum(a)

table=[0]*(10**6)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))

        print(ans)

        table[b]=0
        S=ans
    else:
        print(S)
from collections import defaultdict
def dfs(here,used,match,connect):
    used[here] = 1
    for near in connect[here]:
        m = match[near]
        if m < 0 or (not used[m] and dfs(m,used,match,connect)):
            match[here] = near
            match[near] = here
            return 1
    return 0

a_num, b_num, e_num = (int(n) for n in input().split(" "))
connect = defaultdict(list)
all_v = a_num + b_num
match = [-1 for n in range(all_v)]
for _ in range(e_num):
    v1, v2 = (int(n) for n in input().split(" "))
    v2 += a_num
    connect[v1].append(v2)
    connect[v2].append(v1)
answer = 0
for i in range(all_v):
    if match[i] < 0:
        if dfs(i,used,match,connect):
            used = [0 for n in range(all_v)]
            answer += 1
print(answer)

### FIXED CODE ###

from collections import defaultdict
def dfs(here,used,match,connect):
    used[here] = 1
    for near in connect[here]:
        m = match[near]
        if m < 0 or (not used[m] and dfs(m,used,match,connect)):
            match[here] = near
            match[near] = here
            return 1
    return 0

a_num, b_num, e_num = (int(n) for n in input().split(" "))
connect = defaultdict(list)
all_v = a_num + b_num
match = [-1 for n in range(all_v)]
for _ in range(e_num):
    v1, v2 = (int(n) for n in input().split(" "))
    v2 += a_num
    connect[v1].append(v2)
    connect[v2].append(v1)
answer = 0
for i in range(all_v):
    if match[i] < 0:
        used = [0 for n in range(all_v)]
        if dfs(i,used,match,connect):
            answer += 1
print(answer)
S = input()
K = int(input())

cnt = 0
tmp = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
       tmp += 1
    elif tmp >= 2:
        cnt += tmp // 2
        tmp = 1
    else:
        tmp = 1

if tmp >= 2:
    cnt += 1

tmp1 = cnt
if S[0] == S[-1]:
    if S[-2] != S[-1]:
        cnt += 1

print((cnt * (K - 1) + tmp1))

### FIXED CODE ###

import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
    if tmp >= 2:
        cnt += tmp // 2
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        if tmp >= 3 and tmp % 2 != 0 and S[0] == S[-1]:
            cnt = g1 + (g2 * (K - 1))
        else:
            cnt = (g1 * v) + (g2 * (K - v))

print(cnt)
import math
import copy
H,N=map(int,input().split())

inf=float('inf')
dp=[0]+[inf]*H

for i in range(N):
    A,B=map(int,input().split())
    ndp=copy.deepcopy(dp)
    for j in range(A,H+1):
        ndp[j]=min(ndp[j-A]+B,dp[j])

        if j > H-A:
            ndp[-1]=min(ndp[-1],ndp[j]+B)

    dp=ndp

print(dp[-1])

### FIXED CODE ###

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

N=0
while H > 0:
    N+=1
    H-=A

print(N)
L, R, d = map(int, input().split())
count = 0
for (L,L<=R,L++){
  if (L%2 == 0){
    count +=1
    
  }
}
print(count)

### FIXED CODE ###

L, R, d = map(int, input().split())
count = 0
if ((L and R and d >=1) and (L and R and d <= 100) ):
    for L in range(L, R+1):
        if (L%d == 0):
            count +=1
    print(count)
s = list(map(int, input().split()))
l = len(s)

s1 = [ ((-1)^i + 1)/2 for i in range(l)]
s2 = [ ((-1)^(i+1) + 1)/2 for i in range(l)]

m = 0
n = 0
for i in range(l):
  m += (s[i] - s1[i])^2
  n += (s[i] - s2[i])^2

print(min(m,n))

### FIXED CODE ###

s = input()
l = len(s)

s1 = [ ((-1)**i + 1)//2 for i in range(l)]
s2 = [ ((-1)**(i+1) + 1)//2 for i in range(l)]

m = 0
n = 0
for i in range(l):
  m += (int(s[i]) - s1[i])**2
  n += (int(s[i]) - s2[i])**2

print(min([m,n]))
S,T=map(int,input(),split())
print(T+S)

### FIXED CODE ###

S,T=input().split()
print(T+S)
if val1 % 2 == 0:
        print(int((val1 // 2) * (val1 // 2)))
    else:
        val1 = val1 + 1
        print(int(((val1 // 2) * (val1 // 2)) - (val1 // 2)))

### FIXED CODE ###

n = int(input())
if n&1==0:
    print((n//2)**2)
else:
    print((n//2)*(n//2+1))
n = int(input())
a = [int(input()) for _ in range(n)]
max_a = max(a)
set_a = set(a)
s_set_a = sorted(set(a))[-2]
for i in range(n):
    if max_a == a[i]:
        if len(set_a) == 1:
            print(max_a)
        else:
            print(s_set_a)
    else:
        print(max_a)

### FIXED CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]
max_a = max(a)
set_a = set(a)
max_n = a.count(max_a)
if len(set_a) == 1:
    s_set_a = max_a
else:
    if max_n > 1:
        s_set_a = max_a
    else:
        s_set_a = sorted(set_a)[-2]

for i in range(n):
    if max_a == a[i]:
        print(s_set_a)
    else:
        print(max_a)
import sys
sys.setrecursionlimit(1000000)

from collections import defaultdict

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

dict = defaultdict(int)

for i in range(M):
  B, C = map(int, input().split())
  dict[C] += B

idx = 0
for k, v in sorted(dict.items(), key=lambda x: -x[0]):
  for _ in range(v):
    if A[idx] < k and idx-1 <= len(A):
      A[idx] = k
      idx += 1
    else:
      break
  else:
    continue
  break
      
      
print(sum(A))

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)

from collections import defaultdict

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

dict = defaultdict(int)

for i in range(M):
  B, C = map(int, input().split())
  dict[C] += B

idx = 0
for k, v in sorted(dict.items(), key=lambda x: -x[0]):
  for _ in range(v):
    if idx+1 <= len(A):
      if A[idx] < k:
      	A[idx] = k
      	idx += 1
      else:
        print(sum(A))
        exit()
    else:
      print(sum(A))
      exit()
      
      
print(sum(A))
import sys
input = sys.stdin.readline
import math
import time
import numpy as np

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

k = int(input())

a = prime_factorize(k)

def nana(n):
    for i in range(1,100000):
        na = int("7"*i)
        if na % n ==0:
            return i

t = []
for e in a:
    t.append(nana(e))
print(np.gcd.reduce(t))


### FIXED CODE ###

import sys
input = sys.stdin.readline
import math
import time
import numpy as np

k = int(input())

if k %2 ==0 or k%5==0:
    print(-1)
else:
    if k %7 == 0:
        l = k*9//7
    else:
        l = k*9
    q = 10%l
    for i in range(1,1000000):
        if q ==1:
            print(i)
            break
        else:
            q = (q*10)%l
            pass
a = input()

if a < 1200:
    print("ABC")
elif(a < 2800):
    print("ARC")
else:
    print("AGC")


### FIXED CODE ###

a = int(input())

if a < 1200:
    print("ABC")
elif a < 2800:
    print("ARC")
else:
    print("AGC")
N, M, K = [int(x) for x in input().strip().split()]
A = [int(x) for x in input().strip().split()]
B = [int(x) for x in input().strip().split()]
i = j = 0
ct = 0
while k > 0: 
  if A[i] < B[j] and k - A[i] > 0: 
    k -= A[i]
    i +=1
  elif k - B[j] > 0: 
    k -= B[j]
    j +=1
  ct += 1
print(ct)

### FIXED CODE ###

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
a, b = [0], [0]
for i in range(N):
	a.append(a[i]+A[i])
for i in range(M):
	b.append(b[i]+B[i])
	
ans, j = 0, M
for i in range(N+1):
	if a[i] > K:
		break
	while b[j] > K - a[i]:
		j -= 1
	ans = max(ans, i+j)
	
print(ans)
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t_list = input()
pre_list = []
towin = {"r": "p", "s": "r", "p": "s"}
topoint = {"r": r, "s": s, "p": p}
pre = {}
if s >= p:
    pre["r"] = "s"
else:
    pre["r"] = "p"
if s >= r:
    pre["s"] = "r"
else:
    pre["s"] = "p"
if r >= s:
    pre["p"] = "r"
else:
    pre["p"] = "s"
ans = 0
for i in range(n):
    win_hand = towin[t_list[i]]
    if i < k:
       ans += topoint[win_hand]
       pre_list.append(win_hand)
    else:
        if win_hand == pre_list[i - k]:
            if i <= n - k:
                hand = pre[t_list[i + k]]
                pre_list.append(hand)
        else:
            ans += topoint[win_hand]
            pre_list.append(win_hand)
print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t_list = list(input())
towin = {"r": p, "s": r, "p": s}
ans = 0
for i in range(n):
    if i < k:
        ans += towin[t_list[i]]
    else:
        if t_list[i - k] == t_list[i]:
            t_list[i] = "-1"
        else:
            ans += towin[t_list[i]]
print(ans)
x, y = map(int, input().split())
ans = 0
if x <= 3:
    ans += 400_000 - x * 100_000
if y <= 3:
    ans += 400_000 - y * 100_000
if x == y == 1:
    ans += 400_000
print(ans)


### FIXED CODE ###

x, y = map(int, input().split())
ans = 0
if x <= 3:
    ans += 400000 - x * 100000
if y <= 3:
    ans += 400000 - y * 100000
if x == y == 1:
    ans += 400000
print(ans)
def get_cost(arr, n, k, dp):
  if dp[n] is not None:
    return dp[n]
  elif k >= n-1:
    return abs(arr[-1] - arr[0])
  elif n == 0:
    dp[0] = 0
    return dp[0]
  elif n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]

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

dp = [None]*N
print(get_cost(arr, N-1, K, dp)


### FIXED CODE ###

import sys
sys.setrecursionlimit(500000)

def get_cost(arr, n, dp):
  if dp[n] != float('inf'):
    return dp[n]
  if n == 1:
    dp[1] = abs(arr[1] - arr[0])
    return dp[1]
  elif n == 0:
    dp[0] = 0
    return dp[0]
  else:
    dp[n] = min(
      abs(arr[n] - arr[n-1]) + get_cost(arr, n-1, dp),
      abs(arr[n] - arr[n-2]) + get_cost(arr, n-2, dp)
    )
    return dp[n]


N = int(input())
arr = [int(num) for num in input().split()]
dp_arr = [float('inf')]*N
print(get_cost(arr, N-1, dp_arr))
a,b= map(int,input.split())
print(a*b)

### FIXED CODE ###

a,b = map(int,input().split())
print(a*b)
from collections import defaultdict


N, K, Q = list(map(int, input().split(' ')))
a = [int(input()) for _ in range(N)]

num_ans = defaultdict(int)
for i in a:
    num_ans[i-1] += 1

for i in range(N):
    win = K - (Q - num_ans[i]) > 0
    print('Yes' if win else 'No')



### FIXED CODE ###

S = input()
weather_dic = {'Sunny': 'Cloudy', 'Cloudy': 'Rainy', 'Rainy': 'Sunny'}
print(weather_dic[S])
t = input()

ans = replace("?", "D")
print

### FIXED CODE ###

t = input()
 
ans = t.replace("?", "D")
print(ans)
s = int(input())
n = len(s)
if s[n] == 2,4,5,7,9:
  print('hon')
elif s[n] = 0,1,6,8:
  print('pon')
else:
  print('bon')

### FIXED CODE ###

s = input()
n = len(s)
a = s[n-1:n]
if a == '2' or a == '4' or a == '5' or a == '7' or a == '9':
  print('hon')
elif a == '0' or a == '1' or a == '6' or a == '8':
  print('pon')
else:
  print('bon')
N, M, C = map(int, input().split())
B = {}
A = {}

B.append(map(int, input().split())
for i in range(N):
	A.append(list(map(int, input().split())))


sum = 0
count + 0

for i in range(N):
	sum = 0
	for j in range(M):
		sum += A[i][j]
		sum += B[j]
	sum += C
	if sum > 0:
		count += 1

print(sum)

### FIXED CODE ###

# ファイルをオープンする
#test_data = open("test.txt", "r")

# 行ごとにすべて読み込んでリストデータにする
#line = test_data.readline()


#N, M, C = map(int, line.split())
N, M, C = map(int, input().split())
B = []
A = []

#line = test_data.readline()


#B = [int(b) for b in line.split()]
B = [int(b) for b in input().split()]
for i in range(N):
	A.append(list(map(int, input().split())))
	#line = test_data.readline()
	#A.append(list(map(int, line.split())))

sum = 0
count = 0

for i in range(N):
	sum = 0
	for j in range(M):
		sum += A[i][j] * B[j]
	sum += C
	if sum > 0:
		count += 1

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

if n == 1:
    print(1)
    exit(0)

l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1

if s[-1] == "0":
    l.append(0)

ans = 0
if 2 * k + 1 > len(l):
    sum(l[i * 2 : min(i * 2 + 2 * k + 1, len(l)) : 1])

b = []
b.append(l[0])
for i in range(1, len(l)):
    b.append(b[-1] + l[i])


for i in range((len(l) + 1)):
    if i * 2 + 2 * k + 1 > len(l):
        break
    ans = max(ans, b[min(i * 2 + 2 * k + 1, len(l))] - b[i * 2 - 1])
print(ans)


### FIXED CODE ###

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

if n == 1:
    print(1)
    exit(0)

l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1
        if i == n - 1:
            l.append(temp)

if s[-1] == "0":
    l.append(0)

ans = 0

b = []
b.append(l[0])
for i in range(1, len(l)):
    b.append(b[-1] + l[i])


if 2 * k + 1 > len(l) - 1:
    print(b[len(l) - 1])
    exit(0)
else:
    ans = b[2 * k]


for i in range(1, len(l)):
    if i * 2 + 2 * k > len(l) - 1:
        break
    ans = max(ans, b[min(i * 2 + 2 * k, len(l) - 1)] - b[i * 2 - 1])
print(ans)
import math

n = int(input())
r_n = int(math.sqrt(n))
s = []

for i in range(1, r_n):
    if n%i == 0:
        s.append(n//i + i - 2)
print(min(s))


### FIXED CODE ###

import math

n = int(input())
r_n = int(math.sqrt(n))
s = []

for i in range(1, r_n+1):
    if n%i == 0:
        s.append(n//i + i - 2)
print(min(s))
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k > len(dump_num):
                break;
            if dump_num[k] >= 1:
                break;
            num -=dump_num[k]
        
        if num > ans:
            ans = num
print(ans)
            

### FIXED CODE ###

#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M+1):
    for j in range(M+1-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 0:
                break;
            num -= dump_num[k]
        
        if num >= ans:
            ans = num
print(ans)
n,m=map(int, input().split())
for _ in range (n):
 A=list(map(int, input().split())
for _ in range (m):
 b=int(input())
 for _ in range (n):
  print(sum(a[_][i] * b[i] for i in range(m)))

### FIXED CODE ###

n,m=map(int, input().split())
A=[list(map(int, input().split())) for _ in range (n)]
b=[int(input()) for _ in range (m)]
for _ in range (n):
 print(sum(A[_][i] * b[i] for i in range(m)))
import sys
x,y=map(int,input().split())

for i in range(1,x+1):
  	if y>50: 
      print("No")
      break
    if y==2*(x-i)+4*i:
        print("Yes")
        break
    elif y==2*x+4*(x-i):
        print("Yes")
        break
    else:
        print("No")
        break


### FIXED CODE ###

x,y=map(int,input().split())
c=0
for i in range(0,x+1):
    if y==2*(x-i)+4*i:
        c+=1
        print("Yes")
        break
    elif y==2*i+4*(x-i):
        c+=1
        print("Yes")
        break
if c==0:
    print("No")
str = raw_input()
print str[0] + str(len(str)-2) + str[-1]

### FIXED CODE ###

inp = raw_input()
print inp[0] + str(len(inp) - 2) + inp[-1]
str = input()
#days = ["SUN","MON","TUE","WED","THU","FRI","SAT"]

if str =="SUN":
  print(7)
else if str = "MON":
  print(6)
else if str = "TUE":
  print(5)
else if str = "WED":
  print(3)
else if str = "THU":
  print(3)
else if str = "FRI":
  print(2)
else if str = "SAT":
  print(1)
 

### FIXED CODE ###

string = input()
#days = ["SUN","MON","TUE","WED","THU","FRI","SAT"]

if string =="SUN":
  print(str(7))
elif string == "MON":
  print(str(6))
elif string == "TUE":
  print(str(5))
elif string == "WED":
  print(str(4))
elif string == "THU":
  print(str(3))
elif string == "FRI":
  print(str(2))
elif string == "SAT":
  print(str(1))
N, M = map(int, raw_input().split())
a={}
b={}
for i in range(M):
    k,l=map(int, raw_input().split())
    if k==1:
        a.add(k)
    if l==1: 
        b.add(l)
if a&b: 
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")


### FIXED CODE ###

N, M = map(int, input().split())
a=set()
b=set()
for i in range(M):
    k,l=map(int, input().split())
    if k==1:
        a.add(l)
    if l==N:
        b.add(k)
if a&b:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
A,B = map(int,input().split())
C = [1,2,3]

for i in C:
  if A*B*C % 2 == 1:
    print("Yes")
    exit()
print("No")


### FIXED CODE ###

A,B = map(int,input().split())
C = [1,2,3]

for i in C:
  if (A*B*i) % 2 == 1:
    print("Yes")
    exit()
print("No")
#!/usr/bin/env python3
import sys
import collections


# Generated by 1.1.6 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():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    cur = 1
    c = {}
    route = []
    roop = None
    while (True):
        if cur in c:
            roop = cur
            break
        route.append(cur)
        c[cur] = 1
        cur = A[cur - 1]
    idx = route.index(roop)
    if K <= idx:
        print(route[k])
        return
    route = route[idx:]
    print(route[K % len(route) - idx])
    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
import collections


# Generated by 1.1.6 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():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    cur = 1
    c = {}
    route = []
    roop = None
    while (True):
        if cur in c:
            roop = cur
            break
        route.append(cur)
        c[cur] = 1
        cur = A[cur - 1]
    idx = route.index(roop)
    if K <= idx:
        print(route[K])
        return
    # route = route[idx:]
    print(route[(K - idx) % (len(route) - idx) + idx])
    return


if __name__ == '__main__':
    main()
n = int(input())
g = []
d = [0] * (n * 2)
for i in range(n * 2):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def dfs(u, f, w):
    d[u] = w;
    for go in g[u]:
        if go[0] != f:
            dfs(go[0], u, go[1] + w)
dfs(k, k, 0)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1


### FIXED CODE ###

n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def bfs(u):
    s = []
    s.append(u)
    while s:
        v = s[-1]
        s.pop()
        for go in g[v]:
            if not d[go[0]] and go[0] != u:
                d[go[0]] = d[v] + go[1]
                s.append(go[0])
bfs(k)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1
import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.Queue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[0 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if g[nx][ny] != 0: continue
                    if nx == i and ny == j: continue
                    g[nx][ny] = g[x][y] + 1
                    q.put((nx,ny))
        '''
        print('===========')
        for line in g:
            print(line)
        '''
        longest_path = max(longest_path, max(map(max, g)))
print(longest_path)


### FIXED CODE ###

import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.Queue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[0 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if g[nx][ny] != 0: continue
                    if nx == i and ny == j: continue
                    g[nx][ny] = g[x][y] + 1
                    q.put((nx,ny))
            '''
            print('===========')
            for line in g:
                print(line)
            '''     
            longest_path = max(longest_path, max(map(max, g)))
print(longest_path)
import pandas as pd

n, m = map(int, input().split(' '))
ab = pd.DataFrame(columns=["a", "b"])
for nn in range(n):
    ls = list(map(int, input().split(' ')))
    ab.loc[nn] = ls
    
sortedAb = ab.sort_values("a")
amount = 0
count = 0
for i in range(len(sortedAb)):
    amount += min(sortedAb.iat[i, 1], m - count) * sortedAb.iat[i, 0]
    count += sortedAb.iat[i, 1]
    if count >= m:
        break

print(amount)

### FIXED CODE ###

import numpy as np
 
n, m = map(int, input().split(' '))
a = []; b = []
for nn in range(n):
    ls = list(map(int, input().split(' ')))
    a.append(ls[0])
    b.append(ls[1])

a = np.array(a)
b = np.array(b)

ind = np.argsort(a)
a = a[ind]
b = b[ind]
    
amount = 0
count = 0
for i in range(len(a)):
    amount += min(b[i], m - count) * a[i]
    count += b[i]
    if count >= m:
        break
 
print(amount)
N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N + 1):
    s += L[i]
    if s >= X:
        break

print(i + 1)

### FIXED CODE ###

N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N):
    s += L[i]
    if s > X:
        print(i + 1)
        exit()

print(i + 2)