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

import sys
n,m = [int(x) for x in sys.stdin.readline().split()]
x = [int(x) for x in sys.stdin.readline().split()]
y = [int(x) for x in sys.stdin.readline().split()]
r = 0
mod = 10**9+7
for i in range(n):
	for j in range(i+1,n):
		dx = x[j]-x[i]
		for k in range(m):
			for l in range(k+1,m):
				dy = y[l]-y[k]
				r += dx*dy
tr = r%mod
print(tr)

### FIXED CODE ###

import sys
n,m = [int(x) for x in sys.stdin.readline().split()]
x = [int(x) for x in sys.stdin.readline().split()]
y = [int(x) for x in sys.stdin.readline().split()]
r = 0
mod = 10**9+7
def sumof(a,b):
	s = 0
	for i in range(a):
		s += (2*(i+1)-a-1)*b[i]
		s %= mod
	return s
r = (sumof(n,x)*sumof(m,y))%mod
print(r)
### BUGGY CODE ###

from collections import deque
import itertools
import numpy as np
 
def my_bfs(visited, q, h, w):    
    while q:
        if visited.min() > -1:
            i, j, d = q.pop()
            return d
        i, j, d = q.popleft()
        if i > 0 and visited[i - 1][j] == -1:
            q.append((i - 1, j, d + 1))
            visited[i - 1][j] = d + 1
        if i < h - 1 and visited[i + 1][j] == -1:
            q.append((i + 1, j, d + 1))
            visited[i + 1][j] = d + 1
        if j > 0 and visited[i][j - 1] == -1:
            q.append((i, j - 1, d + 1))
            visited[i][j - 1] = d + 1
        if j < w - 1 and visited[i][j + 1] == -1:
            q.append((i, j + 1, d + 1))
            visited[i][j + 1] = d + 1
    else:
        return d


h , w = map(int, input().split())
visited = np.full((h,w), -1)
q = deque([])
for y in range(h):
    _in = input()
    for x, value in enumerate(_in):
        if value == '#':
            visited[y][x] = 0
            q.append((y, x, 0))
d = my_bfs(visited, q, h, w)
print(d)

### FIXED CODE ###

import numpy as np

H,W = map(int,input().split())
use = np.zeros((H,W),dtype=int)
for i in range(H):
    s = input()
    for j,st in enumerate(s):
        if st == ".":
            use[i][j] = 10**10
        else:
            use[i][j] = 0

for i in range(1,H):
    use[i,:] = np.minimum(use[i,:],use[i-1,:]+1)
for i in reversed(range(H-1)):
    use[i,:] = np.minimum(use[i,:],use[i+1,:]+1)
for j in range(1,W):
    use[:,j] = np.minimum(use[:,j],use[:,j-1]+1)
for j in reversed(range(W-1)):
    use[:,j] = np.minimum(use[:,j],use[:,j+1]+1)

print(np.max(use))
### BUGGY CODE ###

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

if(n == 1):
  print(p)
  exit()
elif(p == 1):
  print(1)
  exit()

num = {}
s, e, i = 0, 0, 2
while s == 0:
  while p%i == 0:
    p //= i
    e += 1
    if(p == 1):
      s = 1
  if(e > 0):
    num[i] = e
  i += 1
  e = 0
cnt = 1
for k, v in num.items():
  if(v >= n):
    cnt *= k ** (v//n)
print(cnt)

### FIXED CODE ###

N, P = map(int,input().split())

def calc(n):
  num = {}
  e, i = 0, 2
  while i**2 <= n:
    while n%i == 0:
      n //= i
      e += 1
    if(e > 0):
      num[i] = e
    i += 1
    e = 0
  return num

if(N == 1):
  print(P)
  exit()
elif(P == 1):
  print(1)
  exit()
else:
  cnt = 1
  for k, v in calc(P).items():
    if(v >= N):
      cnt *= k ** (v//N)
  print(cnt)
### BUGGY CODE ###

mod = 10**9 + 7

def main():
    s = open(0).read()[:-1][::-1]
    n = len(s)

    dp = [[0] * 13 for _ in range(n)]
    if s[0] == '?':
        for j in range(10):
            dp[0][j] = 1
    else:
        dp[0][int(s[0])] = 1

    for i, cur_s in enumerate(s[1:], start=1):
        if cur_s == '?':
            for j in range(10):
                remain = j * 10**i % 13
                for k in range(13):
                    dp[i][(k + remain) % 13] += dp[i-1][k]
        else:
            remain = int(cur_s) * 10**i % 13
            for k in range(13):
                dp[i][(k + remain) % 13] += dp[i-1][k]
    ans = dp[-1][5] % mod
    print(ans)
    return()

if __name__ == '__main__':
    main()


### FIXED CODE ###

MOD = 10**9 + 7

def main():
    s = [int(in_i) if in_i != '?' else -1 for in_i in list(open(0).read())[:-1]]
    n = len(s)

    cnt = [0] * 13
    if s[0] == -1:
        for j in range(10):
           cnt[j] = 1
    else:
        cnt[s[0]] = 1

    for cur_s in s[1:]:
        dp = [0] * 26
        for i in range(13):
            dp[i*10%13] = cnt[i] % MOD
        dp[13:] = dp[:13]

        if cur_s == -1:
            for i in range(13):
                cnt[i] = sum(dp[i+4:i+14])
        else:
            for i in range(13):
                cnt[i] = dp[i+13-cur_s]

    ans = cnt[5] % MOD
    print(ans)
    return()

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

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


if __name__ == '__main__':
    N = int(input())
    prime = prime_factorize(N)
    res = N - 1
    for i in range(2**(len(prime)-1)):
        a = 1
        b = 1
        for j in range(len(prime)):
            if i & (1 << j):
                a *= prime[j]
            else:
                b *= prime[j]
        res = min(res, a+b-2)
    print(res)



### FIXED CODE ###


if __name__ == '__main__':
    N = int(input())
    res = 1
    for i in range(2, int(N**.5)+1):
        if N % i == 0:
            res = i
    print(res + N//res - 2)
### BUGGY CODE ###

N = int(input())
n = [x for x in range(1, N+1)]
m = n[1:]
m.append(n[0])
ans = 0
for i in range(N):
    ans += n[i] % m[i]
print(ans)

### FIXED CODE ###

N = int(input())
print((N+1) * N // 2 - N)
### BUGGY CODE ###

x, y, z, k = 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)

d = []
for aa in a[:k]:
    for bb in b[:k]:
        d.append(aa + bb)

d.sort(reverse=True)

e = []
for dd in d[:k]:
    for cc in c[:k]:
        e.append(dd + cc)

e.sort(reverse=True)

print(*e[:k], sep='\n')


### FIXED CODE ###

# https://www.kumilog.net/entry/python-speed-comp#input-%E3%81%A8-sysstdinreadline
# https://teratail.com/questions/88396

from heapq import heapify, heappush, heappop
import sys

input = sys.stdin.readline


class Data:
    def __init__(self, value, indices):
        self.value = value
        self.indices = indices

    def __lt__(self, other):
        return self.value < other.value


X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
c = map(int, input().split())

a = list(sorted(a, reverse=True))
b = list(sorted(b, reverse=True))
c = list(sorted(c, reverse=True))

hq = []
heapify(hq)

t = a[0] + b[0] + c[0]
t *= -1

d = Data(t, (0, 0, 0))

heappush(hq, d)

dtpls = ((1, 0, 0), (0, 1, 0), (0, 0, 1))

st = set()

cnt = 0
for _ in range(K):
    d = heappop(hq)
    print(-d.value)
    for dtpl in dtpls:
        na = d.indices[0] + dtpl[0]
        if na >= X: continue
        nb = d.indices[1] + dtpl[1]
        if nb >= Y: continue
        nc = d.indices[2] + dtpl[2]
        if nc >= Z: continue

        nt = a[na] + b[nb] + c[nc]
        nt *= - 1

        ntpl = (na, nb, nc)
        if ntpl in st: continue
        heappush(hq, Data(nt, ntpl))
        st.add(ntpl)
### BUGGY CODE ###

import math

def is_prime_sqrt(q): #合成数xはp<=sqrt(x)を満たす素因子pを持つ
    if q == 1: #1は素数ではない
        return False
    if q == 2: #2は素数
        return True
    elif q % 2 == 0: #2以外の偶数は合成数
        return False
    for divisor in range(3, math.floor(math.sqrt(q))+1, 2): #繰り返しの上限は平方根
        if q % divisor == 0: #平方根までに約数があれば合成数
            return False
    return True

Q = int(input())

for i in range(Q):
    l, r = map(int, input().split())
    n = 0
    for q in range(l,r+1,2):
        if is_prime_sqrt(q) and is_prime_sqrt((q+1)/2):
            n += 1
    print(n)


### FIXED CODE ###

import math

def is_prime_sqrt(q): #合成数xはp<=sqrt(x)を満たす素因子pを持つ
    if q == 1: #1は素数ではない
        return False
    if q == 2: #2は素数
        return True
    elif q % 2 == 0: #2以外の偶数は合成数
        return False
    for divisor in range(3, math.floor(math.sqrt(q))+1, 2): #繰り返しの上限は平方根
        if q % divisor == 0: #平方根までに約数があれば合成数
            return False
    return True

def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False

def sieve(n): #nまでの素数をエラトステネスの篩で列挙＝戻り値は各要素が素数のリスト
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return [i for i in range(0,n) if s[i] and i > 1]

def sieveyn(n): #nまでの素数をエラトステネスの篩で列挙＝戻り値は各要素が素数のリスト
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return s


Q = int(input())
A = [0] * 100001 #A[0]からA[100000]までのリスト
A[3] = 1
S = sieveyn(100000)

for i in range(5,100001,4):
    if S[i] and S[int((i+1)/2)]:
        A[i] = 1
for i in range(4,100001):
    A[i] += A[i-1]

#print(A[:100])

for i in range(Q):
    l, r = map(int, input().split())
    print(A[r]-A[l-1])
### BUGGY CODE ###

import numpy as np

L, R = map(int, input().split())

# n mod 2019 の答えは 0 ~ 2018 のいずれか
# それぞれのmodを取った積のmodでも同じ答えになる


def get_ans():
    # 差が2018以上なら積が0の組み合わせが存在する
    if R - L >= 2018:
        return 0

    # 各値のmodを取ってユニークにする
    mods = np.mod(np.arange(L, R + 1), 2019)
    u_mods = np.unique(mods)

    length = len(u_mods)
    # 総当たり
    ans = 2019
    for X in range(length - 1):
        for Y in range(X + 1, length):
            mod = (u_mods[X] * u_mods[Y]) % 2019
            if mod < ans:
                ans = mod
    return ans


print(get_ans())

### FIXED CODE ###

L, R = map(int, input().split())

# n mod 2019 の答えは 0 ~ 2018 のいずれか
# それぞれのmodを取った積のmodでも同じ答えになる


def get_ans():
    # 差が2018以上なら積が0の組み合わせが存在する
    if R - L >= 2018:
        return 0

    # 総当たり
    ans = 2019
    for X in range(L, R):
        for Y in range(X + 1, R + 1):
            ans = min((ans, (X * Y) % 2019))

    return ans


print(get_ans())
### BUGGY CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    for line in stdin:
        n = int(line)
        count = 0

        for a in range((n if n < 1000 else 1000) + 1):
            for ab in range(a, a+1001):
                if ab > n:
                    break

                nab = n - ab
                start = nab if nab < 1000 else 1000
                end = (nab-1001) if (nab-1001) > -1 else -1
                combi = start - end
                if combi >= 0:
                    count += combi

        print(count)
    exit()


if __name__ == '__main__':
    main()

### FIXED CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_combination_list():
    cmb = [0] * 4001
    count = minus = n = s = 0

    for i in range(1001):
        count += (i+1) * (i+2) // 2
        cmb[i] = cmb[4000-i] = count

    for i in range(1001, 2001):
        count += (i+1) * (i+2) // 2
        n += 1
        s += n
        minus += 4 * s
        cmb[i] = cmb[4000-i] = count - minus

    return cmb


def main():
    ans = make_combination_list()
    for line in stdin:
        print(ans[int(line)])


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

n=int(input())
l=[]
for i in range(n):
    s=int(input())
    if s in l:
        l.pop(l.index(s))
    else:
        l.append(s)
print(len(l))

### FIXED CODE ###

n=int(input())
s=set()
for i in range(n):
    s^={input()}
print(len(s))
### BUGGY CODE ###

MOD = 998244353

class BIT:
    def __init__(self, n, init=None):
        self.size = n
        if init is None:
            self.v = [0] * (n + 1) # don't use bit[0]
        else:
            self.v = [0] + init
            for i in range(1, n):
                if i + (i & -i) <= n: self.v[i + (i & -i)] += self.v[i]

    def sum(self, i):
        '''
        Calculate a[1] + ... + a[i]
        '''
        S = 0
        while i > 0:
            S += self.v[i]
            i -= i & -i
        return S
    
    def modsum(self, i, mod):
        '''
        Calculate a[1] + ... + a[i]
        '''
        S = 0
        while i > 0:
            S += self.v[i]
            S %= mod
            i -= i & -i
        return S


    def add(self, i, x):
        '''
        set a[i] to be a[i] + x
        '''
        assert i > 0
        while i <= self.size:
            self.v[i] += x
            i += i & -i

N, K = map(int, input().split())
segments = [tuple(map(int, input().split())) for _ in range(K)]
bit = BIT(N + 1)
bit.v[1] = 1
for i in range(1, N + 1):
    temp = bit.sum(i)
    for s, t in segments:
        if i + s <= N:
            bit.add(i + s, temp)
            bit.add(min(i + t + 1, N + 1), -temp)
print(bit.modsum(N, MOD))

### FIXED CODE ###

MOD = 998244353

N, K = map(int, input().split())
segments = [tuple(map(int, input().split())) for _ in range(K)]
dp = [0] * (2 * N + 1)
for i in range(N):
    for s, t in segments:
        t += 1
        dp[i + s] += dp[i] if i != 0 else 1
        dp[i + t] -= dp[i] if i != 0 else 1
    dp[i + 1] += dp[i]
    dp[i + 1] %= MOD
print(dp[N - 1])
### BUGGY CODE ###

import collections

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

if 0 in A:
  print(0)
  exit()

counter = collections.Counter(A)
ans = 1
for k, v in counter.items():
  ans *= k ** v
if ans > 10**18:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

def main():
  ## IMPORT MODULE
  #import sys

  #sys.setrecursionlimit(100000)
  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n = 2
    A = [1000000000, 1000000000]

  else:
    ##INPUT 
    n = input()
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  if 0 in A:
    print(0)
    exit()
  
  ans = 1
  for a in A:
    ans *= a
    if ans > 10**18:
      ans = -1
      break
  
  print(ans)
    
main()
### BUGGY CODE ###

N, W = map(int, input().split())
X = [0] + [-1] * 110000
ma = 0
for i in range(N):
    w, v = map(int, input().split())
    
    for j in range(len(X))[::-1]:
        if X[j] >= 0:
            if X[j] + w <= W:
                if X[j+v] < 0:
                    X[j+v] = X[j]+w
                    ma = max(ma, j+v)
                else:
                    X[j+v] = min(X[j+v], X[j]+w)

print(ma)

### FIXED CODE ###

N, W = map(int, input().split())
X = [0] + [-1] * 110000
ma = 0
for i in range(N):
    w, v = map(int, input().split())
    
    for j in range(len(X))[::-1]:
        if X[j] >= 0:
            if X[j] + w <= W:
                if X[j+v] < 0:
                    X[j+v] = X[j]+w
                    ma = max(ma, j+v)
                else:
                    X[j+v] = min(X[j+v], X[j]+w)

print(ma)
### BUGGY CODE ###

#coding:utf-8

N = int(input())

A = [int(i) for i in input().split()]

ans = 1

for i in A:
    ans = ans * i
    
if ans > (10 ** 18) :
    ans = -1
    

print("{}".format(ans))

### FIXED CODE ###

#coding:utf-8

N = int(input())

A = [int(i) for i in input().split()]

ans = 1
flag = True

for i in A:
    if i == 0:
        ans = 0
        flag = False

if flag:
    for i in A:
        ans = ans * i
        if ans > (10 ** 18):
            ans = -1
            break   

print("{}".format(ans))
### BUGGY CODE ###

from itertools import product

MOD = 10**9 + 7
S = input()
N = len(S)
dp = [[0 for _ in range(13)] for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    temp = 10**(N-i-1)
    if S[i] == '?':
        for j, k in product(range(13), range(10)):
            rem = (k * temp) % 13
            target = (j + rem) % 13
            dp[i+1][target] += dp[i][j]
            dp[i+1][target] %= MOD
    else:
        rem = (int(S[i]) * temp) % 13
        for j in range(13):
            target = (j + rem) % 13
            dp[i+1][target] += dp[i][j]
            dp[i+1][target] %= MOD
print(dp[N][5])

### FIXED CODE ###

from itertools import product

MOD = 10**9 + 7
S = input()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
for d in range(N):
    if S[d] == '?':
        for i, j in product(range(13), range(10)): # '?' = j
            rem = ((10 * i) % 13 + j) % 13
            dp[d + 1][rem] += dp[d][i]
            dp[d + 1][rem] %= MOD
    else:
        for i in range(13):
            rem = ((10 * i) % 13 + int(S[d])) % 13
            dp[d + 1][rem] += dp[d][i]
            dp[d + 1][rem] %= MOD
print(dp[N][5])
### BUGGY CODE ###

x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  insort(p_list,  int(value))

for value in input().split():
  insort(q_list,  int(value))
  
for value in input().split():
  insort(r_list,  int(value))
  
red = 0
green = 0
white = 0

sum_num = 0
for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1
    
print(sum_num)
  
  
  
  

### FIXED CODE ###

x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
r_list = list(map(int,input().split()))


p_list.sort()
q_list.sort()
r_list.sort()

red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)
### BUGGY CODE ###

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

import itertools

comb = list(itertools.combinations(p, k))

ans = 100000000
for i in range(len(comb)):
    ans = min(ans, sum(comb[i]))

print(ans)
    

### FIXED CODE ###

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

print(sum(p[0:k]))
### BUGGY CODE ###

N, K = list(map(int, input().split()))
p = list(map(int, input().split()))

def kitai(n):
    return (1+n) /2

temp = 0

for i in range(N-K+1):
    add = 0
    for j in range(K):
        add += kitai(p[i+j])
    if temp < add:
        temp = add
        
        
print(temp)

### FIXED CODE ###

N, K = list(map(int, input().split()))
p = list(map(int, input().split()))

def kitai(n):
    return (1+n) /2

temp = 0
S = [0]

for i in range(N):
    S.append(S[-1] + kitai(p[i]))
    
    
temp = 0

for i in range(N-K+1):      
    add = S[i+K] - S[i]
    if add > temp:
        temp = add
    
print(temp)
### BUGGY CODE ###

import math

import math
def Sieve_of_Eratosthenes(N):
    if N<2: return set()
    prime=set(i for i in range(2,N+1))
    for i in range(2,math.ceil(N**0.5)+1):
        tmp=set()
        for j in prime:
            if j>i and j%i==0: tmp.add(j)
        for j in tmp:
            prime.remove(j)

    return prime

def gcd(a,b):
    if b>a:
        a,b=b,a
    while b:
        a,b=b, a%b
    return a

a,b=map(int,input().split())
n=gcd(a,b)
prime=list(Sieve_of_Eratosthenes(int(math.sqrt(n))+2))

ans=1
x=n
for p in prime:
    if x==1:
        break
    if x%p==0:
        ans+=1
        while x%p==0:
            x=x//p

if x!=1: ans+=1
print(ans)


### FIXED CODE ###

import math
def gcd(a,b):
    if b>a:
        a,b=b,a
    while b:
        a,b=b, a%b
    return a

a,b=map(int,input().split())
x=gcd(a,b)
s=set()
for i in range(2, min(x+1,int(math.sqrt(x))+2) ):
    while x%i==0:
        x=x//i
        s.add(i)
    if x==1: break
if x>1: s.add(x)
print(len(s)+1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
A.sort()
B = list(map(int, input().split()))
B.sort()
C = list(map(int, input().split()))
C.sort()
count = [0]*N
answer = 0
for i in range(N):
    x = 0
    for j in range(N):
        if B[i] >= C[N-j-1]:
            break
        x += 1
    count[i] = x

for i in range(N):
    for j in range(N):
        if A[i] >= B[N-j-1]:
            break
        answer += count[N-j-1]
#print(count)
print(answer)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
B = list(map(int, input().split()))
B.sort(reverse=True)
C = list(map(int, input().split()))
C.sort(reverse=True)
count = [0]*N
answer = 0
z = 0
for i in range(N):
    if B[N-1] >= C[i]:
        break
    z += 1
count[N-1] = z
for i in range(1, N):
    k=z
    for j in range(k):
        if B[N-1-i] < C[k-1-j]:
            break
        else:
            z -= 1      
    count[N-1-i] = z
sum_matrix = [0]*(N+1)
y = 0
for i in range(N):
    sum_matrix[i+1] = sum_matrix[i] + count[i]
for i in range(N):
    if A[N-1] >= B[i]:
        break
    answer += count[i]
    y += 1
for i in range(1, N):
    x = y
    for j in range(x):
        if A[N-1-i] < B[x-1-j]:
            break        
        y -= 1
    answer += sum_matrix[y]
print(answer)
### BUGGY CODE ###

from collections import deque

h, w = map(int, input().split())
m = [list(input()) for _ in range(h)]

flag = [[True] * w for _ in range(h)]

q = deque()

for i in range(h):
    for j in range(w):
        if m[i][j] == "#":
            q.appendleft((i, j, 0))
            flag[i][j] = False

ans = 0
while q:
    (i, j, n) = q.pop()
    ans = n
    for dx, dy in zip([1,0, -1, 0], [0, 1, 0, -1]):
        ni = i + dx
        nj = j + dy
        if (0 <= ni < h and 0 <= nj < w and flag[ni][nj]):
            flag[ni][nj] = False
            q.appendleft((ni, nj, n + 1))

print(ans)

### FIXED CODE ###

from collections import deque

h, w = map(int, input().split())
m = [list(input()) for _ in range(h)]

flag = [[True] * w for _ in range(h)]

q = deque()

for i in range(h):
    for j in range(w):
        if m[i][j] == "#":
            q.appendleft((i, j, 0))
            flag[i][j] = False

ans = 0
while q:
    (i, j, n) = q.pop()
    ans = n
    for dx, dy in zip([1,0, -1, 0], [0, 1, 0, -1]):
        ni = i + dx
        nj = j + dy
        if (0 <= ni < h and 0 <= nj < w and flag[ni][nj]):
            flag[ni][nj] = False
            q.appendleft((ni, nj, n + 1))

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

N = int(input())
A = [int(input()) for _ in range(N)]

for i in range(N):
    if i == 0:
        print(max(A[1:]))
        continue
    elif i == N-1:
        print(max(A[:N]))
        continue
    
    a_l = max(A[:i])
    a_h = max(A[i+1:])
    
    if a_l >= a_h:
        print(a_l)
    else:
        print(a_h)
    

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
A_s = sorted(A)[::-1]

for i in A:
    if (A_s[0] == i) & (A_s[1] == i):
        print(A_s[0])
    elif (A_s[0] == i) & (A_s[0] != A_s[1]):
        print(A_s[1])
    else:
        print(A_s[0])
### BUGGY CODE ###


# -*- coding: utf-8 -*-
S = input()
Q = int(input())

rev = False

for q in range(Q):
    i = input().split()
    if i[0] == "1":
        rev = not rev
    elif i[1] == "1" and not rev or i[1] == "2" and rev:
        S = i[2] + S
    else:
        S = S + i[2]

if rev:
    S = S[::-1]
print(S)


### FIXED CODE ###


# -*- coding: utf-8 -*-

S = input()
Q = int(input())

rev = False
S1 = ['0'] * Q
S1i = 0
S2 = ['0'] * Q
S2i = 0

for q in range(Q):
    i = input()
    if i[0] == "1":
        rev = not rev
    elif not rev and i[2] == "1" or rev and i[2] == "2":
        # S = i[4] + S
        S1[S1i] = i[4]
        S1i += 1
    else:
        # S = S + i[4]
        S2[S2i] = i[4]
        S2i += 1

S1 = S1[0:S1i]
S2 = S2[0:S2i]
S1 = S1[::-1]

S = ''.join(''.join(S1) + S + ''.join(S2))

if rev:
    S = S[::-1]

print(S)