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

def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N = int(f_i.readline())
    A = list(map(int, f_i.readline().split()))
    
    ans = 0
    b = 1
    while A:
        next_A = []
        
        bit_cnt = 0
        
        for A_i in A:
            if A_i & 1:
                bit_cnt += 1
            A_i >>= 1
            if A_i:
                next_A.append(A_i)
        
        ans += b * bit_cnt * (N - bit_cnt)
        ans %= mod
        
        b *= 2
        A = next_A[:]
        
    print(ans)

solve()

### FIXED CODE ###

def solve():
    import numpy as np
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N = int(f_i.readline())
    A = np.array(f_i.readline().split(), dtype=np.int64)
    
    ans = 0
    for i in range(60):
        cnt = np.count_nonzero((A >> i) & 1)
        ans += (1 << i) * cnt * (N - cnt)
    
    print(ans % mod)

solve()
### BUGGY CODE ###

import numpy as np
import sys
import math

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

k = int(input())


def gcd(a,b):
    if a > b:
        a , b = b , a

    # q = b / a
    r = b % a

    if r == 0:
        return a
    else:
        return gcd(a,r)
    

sum = 0



for i in range(k):
    for j in range(k):
        for l in range(k):
            sum += np.gcd(np.gcd(i+1,j+1) , l+1 )

print(sum)

### FIXED CODE ###

import numpy as np
import sys
import math

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

data = [2246640
,2332104
,2379085
,2434833
,2490534
,2554600
,2609625
,2693919
,2742052
,2813988
,2875245
,2952085
,3003306
,3096024
,3157249
,3224511
,3306240
,3388576
,3444609
,3533637
,3591322
,3693924
,3767085
,3842623
,3912324
,4027884
,4102093
,4181949
,4270422
,4361548
,4427853
,4548003
,4616104
,4718640
,4812789
,4918561
,5003286
,5131848
,5205481
,5299011
,5392008
,5521384
,5610705
,5739009
,5818390
,5930196
,6052893
,6156139
,6239472
,6402720
,6493681
,6623853
,6741078
,6864016
,6953457
,7094451
,7215016
,7359936
,7475145
,7593865
,7689630
,7886244
,7984165
,8130747
,8253888
,8403448
,8523897
,8684853
,8802826
,8949612
,9105537
,9267595
,9376656
,9574704
,9686065
,9827097
,9997134
,10174780
,10290813
,10493367
,10611772
,10813692]


k = int(input())

if k >= 119:
    print(data[k-119])
    sys.exit()


def gcd(a,b):
    if a > b:
        a , b = b , a

    # q = b / a
    r = b % a

    if r == 0:
        return a
    else:
        return gcd(a,r)
    

sum = 0



for i in range(k):
    for j in range(k):
        for l in range(k):
            sum += gcd(i+1,gcd(j+1,l+1))


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

import math
A, B, N = map(int, input().split())
x = 0
ans1 = 0
while x <= N and x <= 5*10**6:
    ans = int((A*x)/B) - A * int(x / B)
    ans1 = max(ans1, ans)
    x += 1

print(ans1)



### FIXED CODE ###

import math

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


ans1 = 0

if N > B - 1:
    x = B-1
    print(int((A * x) / B) - A * int(x / B))
elif N <= B - 1:
    x = N
    print(int((A * x) / B) - A * int(x / B))
### BUGGY CODE ###

a, b, k = map(int, input().split(' '))

for i in range(k):
    if a != 0:
        a -= 1
    elif b != 0:
        b -= 1

print(a, b)

### FIXED CODE ###

a, b, k = map(int, input().split(' '))

if a != 0 or b != 0:
    if k >= a:
        k -= a
        a = 0
        b = b - k if k <= b else 0
    else:
        a -= k
print(a, b)
### BUGGY CODE ###

N = int(input())

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

checklist = {}

for item in alist:
    if alist.count(item) > 1:
        print('NO')
        exit(0)

print('YES')


### FIXED CODE ###

N = int(input())

alist = input().split()

checklist = {}

for ite in alist:
    if checklist.get(ite) == '1':
        print('NO')
        exit(0)
    else:
        checklist[ite] = '1'

print('YES')
### BUGGY CODE ###

N,Q = map(int,input().split())
s = input()
ans = []
for i in range(Q):
    l,r = map(int,input().split())
    ss = s[l-1:r]
    ans.append(ss.count("AC"))
for i in ans:
    print(i)

### FIXED CODE ###

N,Q = map(int,input().split())
s = input()
t = [0]
ans = []
for i in range(N):
    if s[i:i+2] == "AC":
        t.append(t[i] + 1)
    else:
        t.append(t[i])
for i in range(Q):
    l,r = map(int,input().split())
    ans.append(t[r-1]-t[l-1])
for i in ans:
    print(i)
### BUGGY CODE ###

import bisect


class SegTree:

    def __init__(self, n):
        # supposed that n = 2**m
        INF = 10 ** 12
        self.n = n
        self.val = [INF for _ in range(2 * n + 1)]

    def _update(self, a, b, v, k, l, r):
        if r <= a or b <= l:
            return
        if a <= l and r <= b:
            self.val[k] = min(v, self.val[k])
        else:
            self._update(a, b, v, 2 * k + 1, l, (l + r) // 2)
            self._update(a, b, v, 2 * k + 2, (l + r) // 2, r)

    def update(self, a, b, v):
        # 区間[a, b)上の値がvより大きければvに更新
        self._update(a, b, v, 0, 0, self.n)

    def query(self, x):
        # ノードxの値を返す
        k = self.n + x - 1
        ret = self.val[k]
        while k > 0:
            k = (k - 1) // 2
            ret = min(ret, self.val[k])
        return ret


def main():
    N, Q = list(map(int, input().split(' ')))
    work_list = [list(map(int, input().split(' '))) for _ in range(N)]
    # 工事は出発地から近い順に並べておく
    work_list.sort(key=lambda work: work[2])
    D = [int(input()) for _ in range(Q)]
    # 一番最初に引っかかる工事のインデックスを求める
    QQ = 1
    while QQ < Q:
        QQ <<= 1
    tree = SegTree(QQ)
    for w in range(N):
        s, t, x = work_list[w]
        # s - x <= d < t - x だと工事に引っかかる
        left = bisect.bisect_left(D, s - x)
        right = bisect.bisect_left(D, t - x)
        tree.update(left, right, w)
    for q in range(Q):
        w = tree.query(q)
        if w >= N:
            print(-1)
        else:
            print(work_list[w][2])


if __name__ == '__main__':
    main()


### FIXED CODE ###

import bisect
 
 
class SegTree:
 
    def __init__(self, n):
        # supposed that n = 2**m
        self.n = n
        self.val = [-1] * (2 * n - 1)
 
    def _update(self, a, b, v, k, l, r):
        # 区間[a, b)上の値をvに更新
        if r <= a or b <= l:
            return
        if a <= l and r <= b:
            self.val[k] = v
        else:
            self._update(a, b, v, 2 * k + 1, l, (l + r) // 2)
            self._update(a, b, v, 2 * k + 2, (l + r) // 2, r)
 
    def query(self, x):
        # ノードxの値を返す
        k = self.n + x - 1
        ret = self.val[k]
        while k > 0:
            k = (k - 1) // 2
            ret = max(ret, self.val[k])
        return ret
 
 
def main():
    N, Q = list(map(int, input().split(' ')))
    work_list = [list(map(int, input().split(' '))) for n in range(N)] 
    # 工事は出発地から遠い順に並べておく
    work_list.sort(key=lambda work: work[2], reverse=True)
    D = [int(input()) for q in range(Q)]
    # 一番最初に引っかかる工事のインデックスを求める
    QQ = 1
    while QQ < Q:
        QQ <<= 1
    tree = SegTree(QQ)
    for w, work in enumerate(work_list):
        s, t, x = work
        # s - x <= d < t - x だと工事に引っかかる
        left = bisect.bisect_left(D, s - x)
        right = bisect.bisect_left(D, t - x)
        tree._update(left, right, w, 0, 0, QQ)
    for q in range(Q):
        w = tree.query(q)
        if w == -1:
            print(-1)
        else:
            print(work_list[w][2])
 
 
if __name__ == '__main__':
    main()
### BUGGY CODE ###

# coding: utf-8
# Your code here!
N,M = map(int,input().split())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
LAST = 10**6
MOD = 10**9 + 7
S.append(LAST)
T.append(LAST)
dp = [[0 for j in range(M+2)] for i in range(N+2)]
total = [[0 for j in range(M+2)] for i in range(N+2)]
for i in range(N+1):
    for j in range(M+1):
        I,J = i+1,j+1
        total[I][J] = (total[I][J-1] + total[I-1][J] - total[I-1][J-1]) % MOD
        if S[i] != T[j]:
            continue
        
        dp[I][J] = total[I-1][J-1] + 1
        total[I][J] = (total[I][J] +  dp[I][J]) % MOD
print(dp[N+1][M+1])

### FIXED CODE ###

# coding: utf-8
# Your code here!
N,M = map(int,input().split())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
LAST = 10**6
MOD = 10**9 + 7
S.append(LAST)
T.append(LAST)
dp = [[0 for j in range(M+2)] for i in range(N+2)]
total = [[0 for j in range(M+2)] for i in range(N+2)]
for i in range(N+1):
    for j in range(M+1):
        I,J = i+1,j+1
        total[I][J] = (total[I][J-1] + total[I-1][J] - total[I-1][J-1]) % MOD
        if S[i] != T[j]:
            continue
        
        dp[I][J] = total[I-1][J-1] + 1
        total[I][J] = (total[I][J] +  dp[I][J]) % MOD
print(dp[N+1][M+1])
### BUGGY CODE ###

import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
INF = float("inf")

Q = int(input())
l = [0]*Q
for i in range(Q):
    l[i] = list(map(int,input().split()))

def is_prime(n):
    for i in range(2,int(math.sqrt(n))+1):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return n != 1

for i in range(Q):
    sum = 0
    for j in range(l[i][0],l[i][1]+1):
        if is_prime(j) and is_prime((j+1)//2):
            sum +=1
        else:
            continue
    print(sum)


### FIXED CODE ###

import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
INF = float("inf")

Q = int(input())
l = [0]*Q
Sum = [0]*(10**5+1)
for i in range(Q):
    l[i] = list(map(int,input().split()))

def is_prime(n):
    for i in range(2,int(math.sqrt(n))+1):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return n != 1
for i in range(3,10**5+1,2):
    if is_prime(i) and is_prime((i+1)//2):
        Sum[i] = Sum[i-2]+1
    else:
        Sum[i] = Sum[i-2]

for i in range(Q):
    if l[i][1] ==1:
        print(0)
    elif l[i][0] == 1:
        print(Sum[l[i][1]])
    else:
        print(Sum[l[i][1]]-Sum[l[i][0]-2])
### BUGGY CODE ###

import bisect
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
count = 0
B = sorted(A,key=lambda x:x[1])
h = [x[1] for x in B]
mem = [i+1 for i in range(N-1)]
for i in range(2,N):
    if h[0] + h[1] > i:
        continue
    elif h[-1] + h[-2] < i:
        break
    else:
        for j in range(N-1):
            c = h[j]
            if c >= i:
                break
            else:
                d = i - c
                for k in range(mem[j],N):
                    if h[k] == d:
                        if abs(B[j][0]-B[k][0]) == i:
                            count += 1
                    elif h[k] > d:
                        mem[j] = k
                        break
print(count)

### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
b = Counter([x[0]+x[1]+1 for x in A])
c = Counter([x[0]-x[1]+1 for x in A])
count = 0
for x in b.items():
    count += x[1] * c[x[0]]
print(count)
### BUGGY CODE ###

import itertools
N=int(input())
L=list(map(int, input().split()))
S=0
C=list(itertools.combinations(L,3))
for i in range(len(C)):
    if C[i][0]+C[i][1]>C[i][2] and C[i][1]+C[i][2]>C[i][0] and C[i][2]+C[i][0]>C[i][1]:
        S+=1
print(S)

### FIXED CODE ###

import bisect

N=int(input())
L=sorted(list(map(int, input().split())))
S=0

for i in range(N):
    for j in range(i+1,N):
        S+=bisect.bisect_left(L,L[i]+L[j])-j-1
print(S)
### BUGGY CODE ###

import math
a,b,n=map(int,input().split())
ans=0
for i in range(n):
  c=a*(i+1)//b-a*((i+1)//b)
  ans=max(c,ans)
print(ans)

### FIXED CODE ###

a,b,n=map(int,input().split())
print(a*min(n,b-1)//b-a*(min(b-1,n)//b))
### BUGGY CODE ###

import queue
def main():
    h,w=map(int,input().split())
    maze=[]
    r_append=maze.append
    for i in range(h):
        #r_append=maze.append
        row=input()
        r_append(row)
    #print(maze)
    root=[[-1 for i in range(h*w)] for i in range(h*w)]
    ina=[0]*(h*w)
    for i in range(h):
        for j in range(w):
        # print(maze[i][j])
            now=w*i+j
            hidari=w*i+j-1
            migi=w*i+j+1
            ue=w*(i-1)+j
            sita=w*(i+1)+j
            if maze[i][j]==".":
                if j>0:
                    if maze[i][j-1]!="#":
                        root[now][hidari]=0
                if i+1<h:
                    if maze[i+1][j]!="#":
                        root[now][sita]=0
                if j+1<w:
                    if maze[i][j+1]!="#":
                        root[now][migi]=0
                if 0<i:
                    if maze[i-1][j]!="#":
                        root[now][ue]=0
            else:
                ina[now]=-1
                if j>0:
                    root[now][hidari]=-1
                if i+1<h:
                    root[now][sita]=-1
                if j+1<w:
                    root[now][migi]=-1
                if 0<i:
                    root[now][ue]=-1
    #print(root)
    max=0
    hw=h*w
    for start in range(hw):
        if ina[start]==-1:
           # print(start,"is unreachable")
            continue
        dist=[0]*(hw)
        now=start
        dist[start]=0
        visted=[0]*(hw)
        toVis=queue.Queue()
        visted[now]=1
        for i in range(hw):
            if root[now][i]!=-1:
                toVis.put(i)
                dist[i]=dist[now]+1
        while toVis.empty()!=True:
            now=toVis.get()
            visted[now]=1
            le=dist[now]
            for i in range(hw):
                if root[now][i]!=-1 and visted[i]!=1:
                    toVis.put(i)
                    dist[i]=le+1
        farest=dist[now]
        if max<farest:
            max=farest
    print(max)
            
if __name__=="__main__":
    main()


### FIXED CODE ###

h,w=map(int,input().split())
n=h*w
inf=10**6
field=[]
for i in range(h):
    field.append(input())
dist=[[inf]*n for i in range(n)]
for i in range(h):
    for j in range(w):
        if field[i][j]=="#":continue
        if j+1<w and field[i][j+1]!="#":
            dist[i*w+j][i*w+j+1]=1
            dist[i*w+j+1][i*w+j]=1
        if i+1<h and field[i+1][j]!="#":
            dist[i*w+j][(i+1)*w+j]=1
            dist[(i+1)*w+j][i*w+j]=1
for i in range(n):
    for j in range(n):
        for k in range(n):
            dist[j][k]=min(dist[j][k],dist[j][i]+dist[i][k])
ans=0
for i in range(n):
    for j in range(n):
        if i==j:
            continue
        if dist[i][j]==inf:
            continue
        ans=max(ans,dist[i][j])
print(ans)
### BUGGY CODE ###

N,K = (int(a) for a in input().split())
p = list(map(int,input().split()))
p = list(map(lambda x : (x+1)/2 , p))
s = [0]*(N+1)
for i in range(N) :
    s[i+1] = sum(p[:i+1])

t = [0]*(N-K+1)
for j in range(N-K+1) :
    t[j] = s[j+K] - s[j]

print(max(t))


### FIXED CODE ###

N,K = (int(a) for a in input().split())
p = list((int(a) + 1)/2 for a in input().split())
s = [sum(p[:K])]

for i in range(N-K) :
    s.append(s[-1] - p[i] + p[i+K])
print(max(s))
### BUGGY CODE ###

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


def change(arr):
  newAs = [0] * n
  for i, a in enumerate(arr):
    for t in range(max(i - a, 0), min(i + a + 1, n)):
      newAs[t] += 1
  return newAs


for temp in range(min(k, 10**3)):
  As = change(As)
print(' '.join(map(str, As)))


### FIXED CODE ###

import math
from itertools import accumulate
n, k = map(int, input().split())
As = list(map(int, input().split()))
for i in range(min(41, k)):
  _As = [0]*(n+1)
  for j in range(n):
    l = max(0, j - As[j])
    r = min(n - 1, j + As[j])
    _As[l] += 1
    _As[r+1] -= 1
  As = list(accumulate(_As))[:-1]
print(' '.join([str(A) for A in As]))
### BUGGY CODE ###

N,k=map(int,input().split())
h=list(map(int,input().split()))

INF = float('inf')
dp = [INF] * N
dp[0]=0

for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j])  for j in range(max(i - k, 0), i) )
    
print(dp[-1])

### FIXED CODE ###

import numpy as np

N,k=map(int,input().split())
h = np.array(list(map(int,input().split())))

dp = np.zeros(N, dtype=int)

for i in range(1,N):
    start = max(i-k,0)
    dp[i] = np.min(dp[start:i] + np.abs(h[i] - h[start:i]))
    
print(dp[-1])
### BUGGY CODE ###

from collections import Counter
N = int(input())
S = input()
ans = 0
D = Counter(S)
id = 0
dic = {}
for k in D.keys():
    dic[k] = id
    id += 1
m = 0
OB = [-1]*N
for i in range(N):
    if D[S[i]]<=1:
        pass
    else:
        OB[i] = dic[S[i]]
        m = max(m,dic[S[i]])
SSS = [[] for i in range(m+1)]
for i in range(N):
    SSS[OB[i]].append(i)

for n in range(len(SSS)):
    lss = len(SSS[n])
    for i in range(lss):
        for j in range(lss):
            if i==j or ans>SSS[n][j]-SSS[n][i] or ans>N-SSS[n][j]:
                continue
            id = 0
            while True:
                if SSS[n][i]+id>=N or SSS[n][j]+id>=N or SSS[n][i]+id>=SSS[n][j]:
                    break
                if S[SSS[n][i]+id]==S[SSS[n][j]+id]:
                    id += 1
                else:
                    break
            ans = max(ans,id)
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
ans = 0
right = 0
for left in range(N):
    while S[left:right + 1] in S[right + 1:]:
        ans = max(ans, right + 1 - left)
        right += 1
print(ans)
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
tmp = 1
for i in range(n):
    tmp *= a[i]

if tmp > 10 ** 18:
    print(-1)
else:
    print(tmp)


### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]
tmp = 1
flg = 1
if 0 in a:
    print(0)
else:
    for i in a:
        tmp *= i
        if tmp > 10**18:
            flg = 0
            break
    if flg:
        print(tmp)
    else:
        print(-1)
### BUGGY CODE ###

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

#print(q[len(q) - 2])
exp_sum = []
exp1 = 0
for j in range(0,len(p)-k+1):
    sum1 = 0
    exp1 = 0
#if k == 1:
#for k in range()
#sum1 += sum(p[0:k])
#exp1 = sum1/p[0]
#exp_sum.append(exp1)
#else:
    for i in range(j,j + k):
        sum1 = 0.5 * p[i] * (p[i] + 1)
        exp1 += sum1/p[i]
    exp_sum.append(exp1)
print(max(exp_sum))

### FIXED CODE ###

import sys

def LI(): return list(map(int, sys.stdin.readline().split()))
N, K = LI()
p = LI()
ps = []
for i in range(N):
    ps.append((p[i] + 1) / 2)
 
anss = []
tmp = 0
for i in range(N):
    tmp += ps[i]
    if i > K - 1:
        tmp -= ps[i - K]
    anss.append(tmp)
 
print(max(anss))
### BUGGY CODE ###

N,M = map(int,input().split())
a = [int(input()) for i in range(M)]
Mod = 1000000007
dp = [0]*(N+1) #dp[i]...i番目にいる時の移動方法の数
dp[0] = 1

count = 0
for i in range(1,N):
    if i in a:
        count += 1
        if count == 2:
            print(0)
            exit()
    else:
        count = 0
    
if 1 in a:
    dp[2] = 1
    for i in range(1,N-1):
        dp[i+2] = dp[i+1] + dp[i]
        dp[i+2] %= Mod
        if i+2 in a:
            dp[i+2] = 0
else:
    dp[1] = 1
    for i in range(N-1):
        dp[i+2] = dp[i+1] + dp[i]
        dp[i+2] %= Mod
        if i+2 in a:
            dp[i+2] = 0
print(dp[N])

### FIXED CODE ###

N,M = map(int,input().split())
a = [int(input()) for i in range(M)]
Mod = 1000000007
dp = [1]*(N+1) #dp[i]...i番目にいる時の移動方法の数

count = 0
for i in a:
    if i+1 in a:
        print(0)
        exit()

for i in a:
    dp[i] = 0
if dp[1] == 0:
    for i in range(1,N-1):
        if dp[i+2] != 0:
            dp[i+2] = dp[i+1] + dp[i]
            dp[i+2] %= Mod
else:
    dp[1] = 1
    for i in range(N-1):
        if dp[i+2] != 0:
            dp[i+2] = dp[i+1] + dp[i]
            dp[i+2] %= Mod

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

H,W=map(int,input().split())
INF=H+W-1
a=[]
for i in range(H):
    ai=[]
    Ai=input()
    for j in range(W):
        if Ai[j]=='.':
            ai.append(False)
        else:
            ai.append(True)
    a.append(ai)

# print(a)

c=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if a[i][j]:
            c[i][j]=0
        else:
            c[i][j]=INF

# print(c)

for i in range(H):
    for j in range(W):
        if a[i][j]:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],k)
                k+=1

# print(c)

for i in range(H):
    for j in range(W):
        if 0<c[i][j]<INF:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],c[i][j]+k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],c[i][j]+k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],c[i][j]+k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],c[i][j]+k)
                k+=1

# print(c)

ans=max([max(ci) for ci in c])

print(ans)

### FIXED CODE ###

from collections import deque

class Grid():
    def __init__(self,i,j,isblack):
        self.i=i
        self.j=j
        self.isblack=isblack
        self.distance=0

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

g=[]
for i in range(H):
    Ai=input()
    gi=[]    
    for j in range(W):
        gi.append(Grid(i,j,Ai[j]=='#'))
    g.append(gi)

d=deque()

# 黒マスのreachedをTrueにし，dequeに入れる
for row in g:
    for grid in row:
        if grid.isblack:
            d.append(grid)

while d:
    tg=d.popleft()
    i=tg.i
    j=tg.j
    if i>0 and not g[i-1][j].isblack:
        g[i-1][j].isblack=True
        g[i-1][j].distance=tg.distance+1
        d.append(g[i-1][j])
    if i<H-1 and not g[i+1][j].isblack:
        g[i+1][j].isblack=True
        g[i+1][j].distance=tg.distance+1
        d.append(g[i+1][j])
    if j>0 and not g[i][j-1].isblack:
        g[i][j-1].isblack=True
        g[i][j-1].distance=tg.distance+1
        d.append(g[i][j-1])
    if j<W-1 and not g[i][j+1].isblack:
        g[i][j+1].isblack=True
        g[i][j+1].distance=tg.distance+1
        d.append(g[i][j+1])

'''
for row in g:
    for grid in row:
#        print(grid.distance,end=' ')
#    print()
'''

print(max([max([grid.distance for grid in row]) for row in g]))
### BUGGY CODE ###

N = int(input())
line = list(map(int,input().split()))
a = line[0]
for i in range(len(line)):
    if i == 0:
        pass
    else:
        a = a * line[i]
if a > 10**18:
    print(-1)
else:
    print(a)

### FIXED CODE ###

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

if line.count(0) > 0:
    a = 0
elif line.count(10**18) > 0:
    a = -1
else:
    a = line[0]
    for i in range(len(line)):
        if i == 0:
            pass
        else:
            a = a * line[i]
            if a > 10**18:
                a = -1
                break

print(a)
### BUGGY CODE ###

n=int(input())
a=[]
b=[]
cnt=0
for i in range(n):
  s=input()
  s=sorted(s)
  a.append(s)
  if s not in b:
    b.append(s)
for i in b:
  x=a.count(i)
  if x>0:
    cnt+=x*(x-1)//2
print(cnt)

### FIXED CODE ###

import collections

n=int(input())
lst=[]
ans=0

for i in range(n):
    s=input()
    lst.append(''.join(sorted(s)))

c=collections.Counter(lst)

for i in set(lst):
    x=c[i]

    ans+=x*(x-1)//2


print(ans)