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

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

n = int(input())
s = input()

counter = 0
for p in range(n):
    for q in range(n):
        if p >= q:
            continue
        for r in range(n):
            if q >= r:
                continue
            a = s[p]
            b = s[q]
            c = s[r]
            if a != b and b != c and c != a and r - q != q - p:
                # print(a, b, c, p, q, r)
                counter += 1
print(counter)

### FIXED CODE ###

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

n = int(input())
s = input()

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

counter = r_cnt * g_cnt * b_cnt

for i in range(n):
    for p in range(n):
        j = i + p
        k = j + p
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            counter -= 1
print(counter)
### BUGGY CODE ###

n,k=input().split()
n=int(n)
k=int(k)
a=[int(x) for x in input().split()]


dp=[1e18]*n
dp[0]=0


dp[1]=abs(a[1]-a[0])

m=pow(10,10)

for i in range(2,n):
    m=pow(10,10)
    for j in range(1,k+1):
        if i-j>=0:
            m=min(m,dp[i-j]+abs(a[i]-a[i-j]))
    dp[i]=m
    
    
        

print(dp[n-1])    
        

    
    


### FIXED CODE ###

n,k=input().split()
n=int(n)
k=int(k)
a=[int(x) for x in input().split()]


dp=[1e18]*n
dp[0]=0


dp[1]=abs(a[1]-a[0])

m=pow(10,10)

for i in range(2,n):
    m=pow(10,10)
    for j in range(1,k+1):
        if i-j>=0:
            m=min(m,dp[i-j]+abs(a[i]-a[i-j]))
    dp[i]=m
    
    
        

print(dp[n-1])
### BUGGY CODE ###

a,b,k = map(int,input().split())
for x in range(a,b+1):
  if x<a+k or x>b-k:
    print(x)

### FIXED CODE ###

a,b,k = map(int,input().split())
if b-a<2*k:
  for x in range(a,b+1):
    print(x)
else:
  for x in range(a,a+k):
    print(x)
  for x in range(b-k+1,b+1):
    print(x)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
    b.append(str(a[i]))
    b = b[::-1]
print(" ".join(b))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
a = a[::-1]
odd = a[0::2]
even = a[1::2][::-1]
b = odd + even
print(*b)
### BUGGY CODE ###

def main():
    N, W =map(int, input().split())

    weights = []
    values = []
    for i in range(N):
        wi, vi =map(int, input().split())
        weights.append(wi)
        values.append(vi)

    sum_v = sum(values)

    INF = 10**9+7

    dp = [[INF]*(sum_v+1) for _ in range(N+1)]
    dp[0][0] = 0


    for i in range(N):
        wi = weights[i]
        vi = values[i]
        for j in range(sum_v+1):
            if j < vi:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i][j-vi]+ wi)

    for j in range(sum_v+1):
        if dp[N][j] <= W:
            ans = j

    print(ans)

if __name__ == "__main__":
    main()



### FIXED CODE ###

def main():
    N, W =map(int, input().split())

    weights = []
    values = []
    for i in range(N):
        wi, vi =map(int, input().split())
        weights.append(wi)
        values.append(vi)

    sum_v = sum(values)

    INF = 10**9+7

    dp = [[INF]*(sum_v+1) for _ in range(N+1)]
    dp[0][0] = 0


    for i in range(N):
        wi = weights[i]
        vi = values[i]
        for j in range(sum_v+1):
            if j < vi:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i][j-vi]+ wi)

    for j in range(sum_v+1):
        if dp[N][j] <= W:
            ans = j

    print(ans)

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

import networkx as nx

n, x, y = map(int, input().split())
ans = [0] * (n - 1)

x -= 1
y -= 1
for i in range(n):
    for j in range(n):
        c = min(abs(i - j), abs(i-x)+abs(j-y)+1,abs(i-y)+abs(j-x)+1)
        if c:
            ans[c-1] += 1

for ai in ans:
    print(ai//2)


### FIXED CODE ###

n, x, y = map(int, input().split())
ans = [0] * (n - 1)

x -= 1
y -= 1
for i in range(n):
    for j in range(n):
        c = min(abs(i - j), abs(i-x)+abs(j-y)+1,abs(i-y)+abs(j-x)+1)
        if c:
            ans[c-1] += 1

for ai in ans:
    print(ai//2)
### BUGGY CODE ###

def main():
    n = int(input())
    s = [sorted(input()) for i in range(n)]
    cnt = 0

    while s:
        sa = s.pop()
        cnt += s.count(sa) 
    print(cnt)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    n = int(input())
    s = ["".join(sorted(input())) for i in range(n)]
    dic = {}
    cnt = 0

    for i in s:
        if i in dic:
            cnt+=dic[i]
            dic[i]+=1
        else:
            dic[i]=1

    print(cnt)

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

def main():
  N = int(input())
  Sp = []
  Sm = []
  total = 0
  for i in range(N):
    l = input()
    b = 0
    h = 0
    bm = 0
    for j in range(len(l)):
      if l[j] == '(':
        h += 1
      else:
        h -= 1
      b = min(b, h)
    #if h == 0 and b < 0:
     # bm = min(bm, b)
    if h >= 0:
      Sp.append([b, h])
    elif h < 0:
      Sm.append([b-h, -h])
    total += h
  if total != 0:
    print('No')
    exit()
  Sp.sort(key=lambda x:x[0], reverse=True)
  p = check(Sp)
  if p < 0:
    print('No')
    exit()
  Sm.sort(key=lambda x:x[0], reverse=True)
  m = check(Sm)
  if m < 0:
    print('No')
    exit()
  print('Yes')

def check(S):
  h = 0
  for i in range(len(S)):
    if h + S[i][0] < 0:
      return -1
    else:
      h += S[i][1]
  return h

if __name__ == '__main__':
  main()

### FIXED CODE ###

import sys
readline = sys.stdin.readline

def main():
  N = int(readline())
  Sp = []
  Sm = []
  total = 0
  bm = 0
  for i in range(N):
    l = readline().strip()
    b = 0
    h = 0
    for j in range(len(l)):
      if l[j] == '(':
        h += 1
      else:
        h -= 1
      b = min(b, h)
    if h == 0 and b != 0:
      bm = min(bm, b)
    elif h > 0:
      Sp.append([b, h])
    elif h < 0:
      Sm.append([b-h, -h])
    total += h
  Sp.append([bm, 0])
  if total != 0:
    print('No')
    exit()
  Sp.sort(key=lambda x:x[0], reverse=True)
  p = check(Sp)
  if p < 0:
    print('No')
    exit()
  Sm.sort(key=lambda x:x[0], reverse=True)
  m = check(Sm)
  if m < 0:
    print('No')
    exit()
  print('Yes')

def check(S):
  h = 0
  for i in range(len(S)):
    if h + S[i][0] < 0:
      return -1
    else:
      h += S[i][1]
  return h

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

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))  # 整数のリスト

    next_A = [0 for _ in range(N)]

    for _ in range(K):
        cnt = 0
        if sum(A) == N**2:
            # print("break")
            break
        for i in range(N):
            if A[i] >= N:
                cnt += 1
                # print("continue")
                continue
            for j in range(i-A[i], i+A[i]+1):
                if j >= 0 and j < N:
                    next_A[j] += 1
        next_A = list(map(lambda x: x + cnt, next_A))
        A = next_A
        next_A = [0 for _ in range(N)]

    for i in A:
        print(i, end=" ")

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))  # 整数のリスト
    B = [0] * N

    for j in range(min(K, 41)):
        for i in range(N):
            l = max(0, i-A[i])
            r = min(N-1, i+A[i])
            B[l] += 1
            if r+1 < N: B[r+1] -= 1
        for i in range(1,N):
            B[i] += B[i-1]
        A = B
        B = [0] * N

    for i in A:
        print(i, end=" ")

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

# coding: utf-8
# Your code here!

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

da = [[] for i in range(a)]
d = []
for i in range(b):
    k,kk = map(int,input().split())
    da[k-1].append(kk)
    d.append([k, kk])

for i in range(a):
    da[i].sort()

for i in range(b):
    p = da[d[i][0]-1].index(d[i][1])

    n = str(d[i][0])
    m = str(p+1)

    n = "0"*(6-len(n)) + n
    m = "0"*(6-len(m)) + m

    print(n+m)


### FIXED CODE ###

import sys
input = sys.stdin.readline

a,b = map(int,input().split())
aa = []
d = []
for i in range(b):
    k,kk = map(int,input().split())
    d.append([k, kk,i+1])

da = sorted(d)

kkk = da[0][0]
ll = 0
for i in range(b):
    kkkk = da[i][0]
    if kkk == kkkk:
        ll += 1
    else:
        kkk = kkkk
        ll = 1
    aa.append([da[i][2],da[i][0],ll])
aa.sort()

for i in aa:
    m = str(i[1])
    n = str(i[2])
    m = "0"*(6-len(m))+m
    n = "0"*(6-len(n))+n
    print(m+n)
### BUGGY CODE ###

from collections import deque

def main():
    N, M = map(int, input().split())
    route = [[] for _ in range(N)]

    for i in range(M):
        a, b = map(int, input().split())
        route[a-1].append(b-1)
        route[b-1].append(a-1)
    
    arrive_time = [0] + [-1] * (N - 1)
    arrive_route = [0] + [-1] * (N - 1)
    q = deque()
    q.append(0)
    while len(q):
        idx = q.pop()
        for c in route[idx]:
            if arrive_time[c] >= 0 and arrive_time[c] <= arrive_time[idx]+1:
                continue
            arrive_time[c] = arrive_time[idx]+1
            arrive_route[c] = idx
            q.append(c)
    if min(arrive_time) < 0:
        print("No") 
    else:
        print("Yes")
        for r in arrive_route[1:]:
            print(r+1)


if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import deque, defaultdict
from sys import stdin

input = stdin.readline
def main():
    N, M = map(int, input().split())
    route = defaultdict(list)
    for i in range(M):
        a, b = map(int, input().split())
        route[a-1].append(b-1)
        route[b-1].append(a-1)
    
    arrive_time = [0] + [-1] * (N - 1)
    arrive_route = [0] + [-1] * (N - 1)
    q = deque()
    q.append(0)
    while len(q):
        idx = q.popleft()
        for c in route[idx]:
            if arrive_time[c] >= 0 and arrive_time[c] <= arrive_time[idx]+1:
                continue
            arrive_time[c] = arrive_time[idx]+1
            arrive_route[c] = idx
            q.append(c)
    if min(arrive_time) < 0:
        print("No") 
    else:
        print("Yes")
        for r in arrive_route[1:]:
            print(r+1)


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

#
import random


def main():
    n = int(input())
    agrid = []
    agrid_pointer = [0] * n

    for i in range(n):
        al = list(map(int, input().split()))
        for j in range(len(al)):
            al[j] -= 1
        agrid.append(al)

    ans = 0
    targets = list(range(n))
    random.shuffle(targets)

    while True:
        match = set()
        for i in targets:
            if agrid_pointer[i] == n - 1 or i in match:  # もう全部対戦済み or この日対戦済み
                continue

            target = agrid[i][agrid_pointer[i]]
            if agrid_pointer[target] == n - 1 or target in match:  # もう全部対戦済み or この日対戦済み
                continue
            # i, target は対戦可能

            if agrid[target][agrid_pointer[target]] != i:
                continue

            # can match
            agrid_pointer[i] += 1
            agrid_pointer[target] += 1

            match.add(i)
            match.add(target)

            if len(match) == n or len(match) == n - 1:  # 全員試合することが決まったら
                break

        # print(match)
        if len(match) == 0:  # 対戦がなくなったら
            break
        ans += 1

    # print(aconditions)
    # 対戦が残ってたら
    for p in agrid_pointer:
        if p != n - 1:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#
import sys
import time

def input(): return sys.stdin.readline().strip()

def main():
    n = int(input())
    agrid = []
    agrid_pointer = [0] * n

    start = time.time()

    for i in range(n):
        al = list(map(int, input().split()))
        agrid.append(al)

    ans = 0

    while True:
        match = set()
        for i in range(n):
            if agrid_pointer[i] == n - 1 or i in match:  # もう全部対戦済み or この日対戦済み
                continue

            target = agrid[i][agrid_pointer[i]] - 1
            if agrid_pointer[target] == n - 1 or target in match:  # もう全部対戦済み or この日対戦済み
                continue
            # i, target は対戦可能

            if agrid[target][agrid_pointer[target]] - 1 != i:
                continue

            # can match
            agrid_pointer[i] += 1
            agrid_pointer[target] += 1

            match.add(i)
            match.add(target)

            if len(match) == n or len(match) == n - 1:  # 全員試合することが決まったら
                break

        # print(match)
        if len(match) == 0:  # 対戦がなくなったら
            break
        elapsed_time = time.time() - start
        if elapsed_time > 1.5:
            print(n * (n-1)//2)
            return

        ans += 1

    # print(aconditions)
    # 対戦が残ってたら
    for p in agrid_pointer:
        if p != n - 1:
            print(-1)
            return
    print(ans)


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

from sys import stdin
import fractions
 
N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) // 2 for x in stdin.readline().rstrip().split()]
ans = 0
g = 1
 
for i in range(len(A)):
    g = g * A[i] // fractions.gcd(g, A[i])
 
for i in range(len(A)):
    if (g // A[i]) % 2 == 0:
        print(0)
        exit()
 
ans = M // g
if ans % 2 == 0:
    print(ans // 2)
else:
    print((ans + 1) // 2)


### FIXED CODE ###

from sys import stdin
import fractions

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) // 2 for x in stdin.readline().rstrip().split()]
ans = 0
g = 1

for i in range(len(A)):
    g = g * A[i] // fractions.gcd(g % A[i], A[i])
    if g > M:
        print(0)
        exit()

for i in range(len(A)):
    if (g // A[i]) % 2 == 0:
        print(0)
        exit()

ans = M // g
if ans % 2 == 0:
    print(ans // 2)
else:
    print((ans + 1) // 2)
### BUGGY CODE ###

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

mul = [1]
for i in range(n):
    mul.append(mul[i] * A[i])
#print(mul)
output = []
for i in range(k+1, n+1):
    if mul[i]/mul[i-k] > mul[i-1]/mul[(i-1)-k]: output.append("Yes")
    else: output.append("No")
print(*output, sep="\n")

### FIXED CODE ###

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

output = []
count = 0
for i in range(k, n):
    if A[i] > A[count]:
        output.append("Yes")
    else:
        output.append("No")
    count += 1
    
print(*output, sep="\n")
### BUGGY CODE ###

from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[]
        self.dict=defaultdict(lambda:[])
        for i in range(self.n):
            self.roots.append(i)
            self.dict[i].append(i)

    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                #self.dict[self.root(y)]=self.dict[self.root(y)]+self.dict[self.root(x)]
                #self.dict[self.root(x)]=[]
                self.roots[self.root(x)]=self.root(y)
                #self.allunite(self.root(x),self.root(y))
            else:
                #self.dict[self.root(x)]=self.dict[self.root(x)]+self.dict[self.root(y)]
                #self.dict[self.root(y)]=[]
                self.roots[self.root(y)]=self.root(x)
                #self.allunite(self.root(y),self.root(x))
    

    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    count[str(l)]+=1
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    print(count[str(l)],end=" ")


### FIXED CODE ###

from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")
### BUGGY CODE ###



from collections import deque

N = int(input())
A = [deque(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]

match_order = {k:v for k,v in enumerate(A)}

cnt_finished = 0

days = 1
flag = True
while flag:
    flag = False
    used = set()
    complete = []
    for i, order in match_order.items():

        if not order:
            continue
        if i in used:
            continue
        j = order[0]
        if j in used:
            continue
        # それぞれの人について、残りの順番で初めに戦う相手がお互いになっている = その段階で試合ができる
        if A[j][0] == i:
            A[i].popleft()
            A[j].popleft()
            used.add(i)
            used.add(j)
            flag = True
            # N-1人と戦い終わった人は次以降調べない
            if not A[i]:
                cnt_finished += 1
                complete.append(i)
            if not A[j]:
                cnt_finished += 1
                complete.append(j)
            #print(i,j, cnt_finished)

    if not flag:
        print(-1)
        exit()
    
    if cnt_finished == N:
        print(days)
        exit()
    else:
        for i in complete:
            del match_order[i]
        days += 1

### FIXED CODE ###


from collections import deque

N = int(input())
match_order = [deque(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]


q = deque(range(N))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    if next_opponent[b] == a:
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で戦う相手がaではないなら、順番が来るまで待つ
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print(-1)
else:
    print(max(finish_by))
### BUGGY CODE ###

N, M = map(int, input().split())
A = list(int(i/2) for i in map(int, input().split()))
m = max(A)
A.remove(m)
cnt = 0
for i in range(m,M+1,2*m):
	semi = True
	for j in A:
		if i not in range(j,M+1,2*j):
			semi = False
			break
	if semi:
		cnt += 1

print(cnt)

### FIXED CODE ###

N, M = map(int, input().split())
A = set(i//2 for i in map(int, input().split()))
B = []
for i in A:
	B = B + [0]
	while i % 2 == 0:
		i //= 2
		B[-1] += 1
if len(set(B)) == 1:
	m = max(A)
	A.remove(m)
	base = 0
	for i in range(m,M+1,2*m):
		semi = True
		for j in A:
			if i % j != 0 or (i // j) % 2 == 0:
				semi = False
				break
		if semi:
			base = i
			break
	if base == 0:
		print(0)
	else:
		print(((M // base) + 1)//2)
else:
	print(0)
### BUGGY CODE ###

h, w, d = map(int, input().split(' '))
matrix = [list(map(int, input().split(' '))) for _ in range(h)]
map2 = {}
for i in range(h):
    for j in range(w):
        map2[matrix[i][j]] = [i, j]
        
q = int(input())
lr = [list(map(int, input().split(' '))) for _ in range(q)]


for l, r in lr:
    cnt = 0
    x, y = map2[l]
                
    while l != r:
        l = l + d
        i, j = map2[l]
        cnt += abs(x - i) + abs(y - j)
        x = i
        y = j
                    
    print(cnt)

### FIXED CODE ###

h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])
### BUGGY CODE ###

import copy
n = int(input())
a = [int(input()) for _ in range(n)]

for i in range(n):
    a_ = copy.copy(a)
    a_.pop(i)
    print(max(a_))

### FIXED CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]
a_ = sorted(a)
a_max = a_[-1]
a_sec = a_[-2]
for i in range(n):
    if a[i] == a_max:
        print(a_sec)
    else:
        print(a_max)
### BUGGY CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import deque

H, W = map(int,input().split())
sth, stw = 0, 0
glh, glw = H-1, W-1

INF = -1
Gmap = [list(input()) for _ in range(H)]
Dist = [[INF]*W for _ in range(H)]
direc = {(1,0), (-1,0), (0,1), (0,-1)}
next_q = deque([])

def dfs_settings():
    #複数
    for h in range(H):
        for w in range(W):
            if Gmap[h][w]=='#':
                next_q.append([h,w])
                Dist[h][w]=0

def dfs():
    ret = 0
    while len(next_q)!=0:
        #スタック取り出し(先頭)幅優先
        h,w = next_q.popleft()
        hs, ws = h + 0 , w + 1
        if ws<W and Gmap[hs][ws]=='.' and Dist[hs][ws]==INF:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h + 1 , w + 0
        if hs<H and Gmap[hs][ws]=='.' and Dist[hs][ws]==INF:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h +0 , w - 1
        if ws>=0 and Gmap[hs][ws]=='.' and Dist[hs][ws]==INF:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h - 1 , w + 0
        if hs>=0 and Gmap[hs][ws]=='.' and Dist[hs][ws]==INF:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
    return ret

def main():
    dfs_settings()
    ret = dfs()
    print(ret)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import deque

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

INF = -1
Gmap = [list(input()) for _ in range(H)]
next_q = deque([])

def dfs_settings():
    #複数
    for h in range(H):
        for w in range(W):
            if Gmap[h][w]=='#':
                next_q.append([h,w])
                Gmap[h][w]=0

def dfs():
    ret = 0
    while len(next_q)!=0:
        #スタック取り出し(先頭)幅優先
        h,w = next_q.popleft()
        lv=Gmap[h][w]+1
        hs, ws = h + 0 , w + 1
        if ws<W and Gmap[hs][ws]=='.':
            next_q.append([hs,ws])
            Gmap[hs][ws] = lv
            ret = max(ret, Gmap[hs][ws])
        hs, ws = h + 1 , w + 0
        if hs<H and Gmap[hs][ws]=='.':
            next_q.append([hs,ws])
            Gmap[hs][ws] = lv
            ret = max(ret, Gmap[hs][ws])
        hs, ws = h + 0 , w - 1
        if ws>=0 and Gmap[hs][ws]=='.':
            next_q.append([hs,ws])
            Gmap[hs][ws] = lv
            ret = max(ret, Gmap[hs][ws])
        hs, ws = h - 1 , w + 0
        if hs>=0 and Gmap[hs][ws]=='.':
            next_q.append([hs,ws])
            Gmap[hs][ws] = lv
            ret = max(ret, Gmap[hs][ws])
    return ret

def main():
    dfs_settings()
    ret = dfs()
    print(ret)

if __name__ == '__main__':
    main()