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

from collections import deque
h, w = map(int,input().split())
graph = []
finishied = [[False for _ in range(w)] for _ in range(h)]
y = [0, 0, 1,-1]
x = [1,-1, 0, 0]
q = deque([])
for i in range(h):
    a = input()
    graph.append(list(a))
    for j in range(w):
        if a[j] == "#":
            q.append((i,j,0))
while q:
    c = q.popleft()
    for i in range(4):
        nh = c[0]+y[i]
        nw = c[1]+x[i]
        if nh < 0 or nh >= h or nw < 0 or nw >= w or graph[nh][nw] == "#":continue
        graph[nh][nw] = "#"
        q.append((nh,nw,c[2]+1))
print(c[2])

### FIXED CODE ###

from collections import deque
h, w = map(int,input().split())
graph = []
finished = [[False for _ in range(w)] for _ in range(h)]
y = [0, 0, 1,-1]
x = [1,-1, 0, 0]
q = deque([])
for i in range(h):
    a = input()
    graph.append(a)
    for j in range(w):
        if a[j] == "#":
            q.append((i,j,0))
while q:
    c = q.popleft()
    for i in range(4):
        nh = c[0]+y[i]
        nw = c[1]+x[i]
        if nh < 0 or nh >= h or nw < 0 or nw >= w or graph[nh][nw] == "#" or finished[nh][nw]:continue
        finished[nh][nw] = True
        q.append((nh,nw,c[2]+1))
print(c[2])
### BUGGY CODE ###

N=int(input())
A=[]
for i in range(N):
    a=int(input())
    if a not in A:
        A.append(a)
    else:
        A.remove(a)
print(len(A))

### FIXED CODE ###

N=int(input())
A=set()
for i in range(N):
    a=int(input())
    if a not in A:
        A.add(a)
    else:
        A.remove(a)
print(len(A))
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()

for _ in range(Q):
    l, r = map(int, input().split())
    print(S[l-1:r].count('AC'))


### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

t = [0] * (N+2)
t[0] = 1 if S[:2] == 'AC' else 0

for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i + 2] == 'AC' else 0)

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

N=int(input())

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

NA=[]

for i in range(N):
    NA.append(A[i]-i)

cnt=0
for i in range(N-1):
    cnt+=NA.count(-i-A[i])

print(cnt)



### FIXED CODE ###

N=int(input())

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

NA=[]

for i in range(N):
    NA.append(A[i]-i)

from collections import Counter

d=Counter(NA)

cnt=0
for i in range(N-1):
    cnt+=d[-i-A[i]]

print(cnt)
### BUGGY CODE ###

import sys
from itertools import combinations

def main():
    n, s = sys.stdin.read().split()
    n = int(n)
    res = set()
    for comb in combinations(s, 3):
        res.add(comb)
    
    ans = len(res)
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
from itertools import combinations, product
from string import digits
from collections import Counter

def main():
    n, s = sys.stdin.read().split()
    n = int(n)
    
    # t = [None] * (n+1)
    # c = Counter(s)
    # t[0] = set(s)
    # # for i in range(n):
    # #     c[s[i]] -= 1
    # #     if c[s[i]] == 0:
    # #         t[i+1] = t[i] - set([s[i]])
    # #     else:
    # #         t[i+1] = t[i]
    # # print(t)


    
    cand = list(''.join(p) for p in product(digits, repeat=3))
    # print(cand)
    # for comb in combinations(s, 3):
    #     res.add(comb)
    
    # ans = len(res)
    # print(ans)

    res = set()
    for c in cand:
        i = s.find(c[0])
        if i == -1:
            continue
        j = s.find(c[1], i+1)
        if j == -1:
            continue
        k = s.find(c[2], j+1)
        if k == -1:
            continue
        
        res.add(c)
        
    ans = len(res)
    print(ans)

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

import heapq
h,w,k = list(map(int, input().split()))
x1,y1,x2,y2 = list(map(int, input().split()))
x1,y1,x2,y2 = x1-1,y1-1,x2-1,y2-1
c = [[] for _ in range(h)]
dp = [[0]*w for _ in range(h)]
que = []
for i in range(h):
  c[i] = input()

dp[x1][y1] = 1
heapq.heappush(que, (dp[x1][y1], [x1,y1]))
dx = [1,0,-1,0]
dy = [0,1,0,-1]
while que:
  p= heapq.heappop(que)
  p = p[1]
  if (p[0] == x2) &( p[1] == y2):break
  for i in range(4):
    nx = p[0] + dx[i]
    ny = p[1] + dy[i]
    if (nx>=0) & (nx<=(h-1)) & (ny>=0) & (ny<=(w-1)):
      if c[nx][ny] != '@':
        cnt = 1
        while (0 <= dp[nx][ny] < dp[p[0]][p[1]]):
          dp[nx][ny] = dp[p[0]][p[1]] + 1
          heapq.heappush(que, (dp[nx][ny], [nx,ny]))
          cnt += 1
          nnx = nx + dx[i]
          nny = ny + dy[i]
          if (nnx<0) | (nnx>(h-1)) | (nny<0) | (nny>(w-1)) | (cnt > k):break
          if c[nnx][nny] == '@':break
          nx = nnx
          ny = nny

if dp[x2][y2] != 0:
  print(dp[x2][y2]-1)
else:
  print(-1)



### FIXED CODE ###

#import heapq
from collections import deque
h,w,k = list(map(int, input().split()))
x1,y1,x2,y2 = list(map(int, input().split()))
x1,y1,x2,y2 = x1-1,y1-1,x2-1,y2-1
dp = [[-1]*w for _ in range(h)]
#que = []
que = deque()
c = [input() for _ in range(h)]

dp[x1][y1] = 0
#heapq.heappush(que, (dp[x1][y1], [x1,y1]))
que.append((x1,y1))
dx = [1,0,-1,0]
dy = [0,1,0,-1]
while que:
  x,y=que.popleft()
  if x==x2 and y==y2:
    exit(print(dp[x][y]))
  for dx,dy in [[1,0],[-1,0],[0,-1],[0,1]]:
    for i in range(1,k+1):
      xx=x+dx*i
      yy=y+dy*i
      if not(0<=xx<h and 0<=yy<w) or c[xx][yy]=="@":break
      if 0<=dp[xx][yy]<=dp[x][y]:break
      if dp[xx][yy]==-1:que.append((xx,yy))
      dp[xx][yy]=dp[x][y]+1
print(-1)
### BUGGY CODE ###

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

num = sum(A[0:K])
ind = 0
max_num =0
for i in range(N-K):
    num -= A[i]
    num += A[i+K]
    if num> max_num:
        ind = i+1
        max_num = num

ans = 0
for i in range(ind,ind+K):
    ans += sum(range(1,A[i]+1))/A[i]
    
print(ans)

### FIXED CODE ###

N,K = map(int, input().split())
A=list(map(int, input().split()))  
 
num = sum(A[0:K])
ind = 0
max_num =0
for i in range(N-K):
    num -= A[i]
    num += A[i+K]
    if num> max_num:
        ind = i+1
        max_num = num
 
ans = 0
for i in range(ind,ind+K):
    ans += (1+A[i])/2
    
print(ans)
### BUGGY CODE ###

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

result = []
if h*w<=k:
    result.append(0)
else:
    for i in range(2**(h-1)):
        use_s = s.copy()
        checker, num = 0, i
        while num > 0:
            checker += num%2
            num >>= 1
        x = 0
        c = [0]*(checker+1)

        for j in range(w):
            num = i
            p = 0
            nex = [0]*(checker+1)
            for m in range(h):
                nex[p] += use_s[m]%10
                use_s[m] //= 10
                p += num%2
                num = num >> 1          
            if max(nex)>k:
                x = 10**5
                break

            if all(nex[m]+c[m] <= k for m in range(checker+1)):
                c = [c[I]+nex[I] for I in range(checker+1)]
            else:
                x += 1
                c = nex
        result.append(checker+x)
print(min(result))

### FIXED CODE ###

h, w, k = map(int, input().split())
s = [list(map(int, list(input()))) for _ in range(h)]
result = []
if h*w<=k:
    result.append(0)
else:
    for i in range(2**(h-1)):
        checker, num = 0, i
        while num > 0:
            checker += num%2
            num >>= 1
        x = 0
        c = [0 for _ in range(checker+1)]
        
        num = i
        p = 0
        nex = [0 for _ in range(checker+1)]
        for m in range(h):
            nex[p] += s[m][0]
            p += num%2
            num = num >> 1
        if max(nex) > k:
            x = float('inf')
            continue

        if all(nex[m]+c[m] <= k for m in range(checker+1)):
            c = [c[I]+nex[I] for I in range(checker+1)]
        else:
            x += 1

        for j in range(1, w):
            num = i
            p = 0
            nex = [0 for _ in range(checker+1)]
            for m in range(h):
                nex[p] += s[m][j]
                p += num%2
                num = num >> 1
            if max(nex) > k:
                x = float('inf')
                break            
            
            if all(nex[m]+c[m] <= k for m in range(checker+1)):
                c = [c[I]+nex[I] for I in range(checker+1)]
            else:
                x += 1
                c = nex
        result.append(checker+x)
print(min(result))
### BUGGY CODE ###

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1

cnt_list = [0] * len(a_cnt)
for i, value in enumerate(a_cnt.values()):
    cnt_list[i] = value

cnt_list.sort()

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.pop(0)
print(ans)


### FIXED CODE ###

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1
from  operator import itemgetter

a_cnt = sorted(a_cnt.items(), key=itemgetter(1))

from collections import deque
cnt_list = deque([])
for i, j in a_cnt:
    cnt_list.append(j)

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.popleft()
print(ans)
### BUGGY CODE ###

N, X, Y = map(int, input().split(" "))

counts = [0 for i in range(N-1)]

distances = [[] for i in range(N-1)] 

for i in range(N):
    for j in range(i+1, N):
        dis = j-i
        if i <= X-1 :
            if j >= Y-1:
                dis -= Y-X-1
            elif j < Y-1:
                dis = min([j-i, (X-1)-i+1+abs(j-(Y-1))])             
        else:
            dis = min([j-i, i-(X-1)+1+abs(j-(Y-1))])
        
        distances[i].append(dis)
for i in range(N-1):
    for j in range(N-i-1):
        k = distances[i][j]-1
        counts[k] += 1
        
for k in range(N-1):
    print(counts[k])

### FIXED CODE ###

N, X, Y = map(int, input().split(" "))

counts = [0 for i in range(N-1)]

for i in range(N):
    for j in range(i+1, N):
        if i <= X-1 :
            if j >= Y-1:
                dis = j-i -(Y-X-1)
            elif j < Y-1:
                dis = min([j-i, (X-1)-i+1+abs(j-(Y-1))])
                
        else:
            dis = min([j-i, i-(X-1)+1+abs(j-(Y-1))])
        
        # distances[i].append(dis)
        k = dis-1
        counts[k] += 1
        
for k in range(N-1):
    print(counts[k])
### BUGGY CODE ###

class SegTree:

    def __init__(self, N, S):
        self.N = 1
        while self.N < N:
            self.N *= 2
        self.tree = [set() for i in range(2 * self.N - 1)]
        for i in range(len(S)):
            self.tree[self.N - 1 + i] = set(S[i])
        for i in range(self.N - 2, -1, -1):
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def update(self, index, char):
        i = self.N - 1 + index
        self.tree[i] = set(char)

        while i > 0:
            i = (i - 1) // 2
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def get(self, a, b):
        return self.query(a, b, 0, 0, self.N)

    def query(self, a, b, k, l, r):
        if r <= a or b <= l:
            return set()

        if a <= l and r <= b:
            return self.tree[k]

        left = self.query(a, b, 2 * k + 1, l, (l + r) // 2)
        right = self.query(a, b, 2 * k + 2, (l + r) // 2, r)
        return left | right


def solve():

    N = int(input())
    S = list(input())
    tree = SegTree(N, S)

    Q = int(input())
    for _ in range(Q):
        t_q, in_1, in_2 = input().split()
        if t_q == '1':
            i_q = int(in_1)
            c_q = in_2
            tree.update(i_q-1, c_q)
        elif t_q == '2':
            l_q = int(in_1)
            r_q = int(in_2)
            print(len(tree.get(l_q-1, r_q)))


solve()


### FIXED CODE ###

class SegTree:

    def __init__(self, N, S):
        self.N = 1
        while self.N < N:
            self.N *= 2
        self.tree = [0 for i in range(2 * self.N - 1)]
        for i in range(len(S)):
            self.tree[self.N - 1 + i] = 1 << (ord(S[i]) - ord('a'))
        for i in range(self.N - 2, -1, -1):
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def update(self, index, char):
        i = self.N - 1 + index
        self.tree[i] = 1 << (ord(char) - ord('a'))

        while i > 0:
            i = (i - 1) // 2
            self.tree[i] = self.tree[i * 2 + 1] | self.tree[i * 2 + 2]

    def get(self, a, b):
        return self.query(a, b, 0, 0, self.N)

    def query(self, a, b, k, l, r):
        if r <= a or b <= l:
            return 0
        elif a <= l and r <= b:
            return self.tree[k]
        else:
            left = self.query(a, b, 2 * k + 1, l, (l + r) // 2)
            right = self.query(a, b, 2 * k + 2, (l + r) // 2, r)
            return left | right


def solve():

    N = int(input())
    S = list(input())
    tree = SegTree(N, S)

    Q = int(input())
    for _ in range(Q):
        t_q, in_1, in_2 = input().split()
        if t_q == '1':
            i_q = int(in_1)
            c_q = in_2
            tree.update(i_q-1, c_q)
        elif t_q == '2':
            l_q = int(in_1)
            r_q = int(in_2)
            print(bin(tree.get(l_q-1, r_q)).count('1'))


solve()
### BUGGY CODE ###

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

# 各数値の重複数辞書
counter = collections.Counter(a)

# 組み合わせ数
def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair

# 各数字を抽出しない時と抽出した時の組み合わせ数
fulldic = dict()
reducedic = dict()
for k, v in counter.items():
    fulldic[k] = combi(v)
    reducedic[k] = combi(v-1)
    
for el in a:
    print(sum(fulldic.values()) - (fulldic[el] - reducedic[el]))

### FIXED CODE ###

import collections
n = input()
a = list(input().split())

# 各数値の重複数辞書
counter = collections.Counter(a)

# 組み合わせ数
def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair


total = sum([combi(v) for v in counter.values()])
for el in a:
    print(total - (counter[el] - 1))
### BUGGY CODE ###

#尺取り法と累積和
n, k = map(int, input().split())
a = list(map(int, input().split()))
#kを法とする配列を作る
a = [i % k for i in a]
acc_a = [0 for i in range(n+1)]
acc_a[0] = 0
acc_a[1] = a[0]
#kを法とする累積和
for i in range(2,len(a) + 1):
    acc_a[i] = (a[i - 1] + acc_a[i-1]) % k

"""
#尺取り法、
right = 1
left = 0
ans = 0
while right != len(acc_a) or right != left:
    print("l{}  r{}".format(left, right))
    #選んだ配列の個数
    list_num = 1 + right - left
    #選ぶ配列長さがkより大きいことはない
    if list_num >= k:
        left += 1
    #左が追い付いたら右を増やす
    if right == left:
        right += 1
    sum_list = acc_a[right] - acc_a[left]
    if sum_list < 0:
        sum_list += k
    
    if sum_list == list_num:
        ans += 1
        right += 1
    else:
        left += 1
"""
#print(acc_a)
ans = 0
for i in range(len(acc_a)):
    for j in range(max(0, i - k), i):
        #print("i {}  j {}".format(i,j))
        if i - j > k:
            break
        list_sum = acc_a[i] - acc_a[j]
        if list_sum < 0:
            list_sum += k
        if i - j == list_sum:
            #print("i {}  j {}".format(i,j))
            ans += 1


print(ans)
    


### FIXED CODE ###

from collections import deque
n, k = map(int, input().split())
a = list(map(int, input().split()))
#kを法とする配列を作る
a = [(i - 1) for i in a]
acc_a = [0 for i in range(n+1)]
acc_a[0] = 0
acc_a[1] = a[0] % k
#kを法とする累積和
for i in range(2,len(a) + 1):
    acc_a[i] = (a[i - 1] + acc_a[i-1]) % k
ans = 0
count = {}
q = deque()
for i in acc_a:
    if i not in count:
        count[i] = 0
    ans += count[i]
    count[i] += 1
    q.append(i)
    if len(q) == k:
        count[q.popleft()] -= 1
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3

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

    max_i = 0
    for i in range(1, max(a) + 1):
        if i in a:
            ind = a.index(i)
            if ind >= 0:
                a = a[ind:]
                max_i = i
            else:
                break
        else:
            break
        
    if max_i == 0:
        print(-1)
    elif max_i == N:
        print(0)
    else:
        print(N - max_i)


main()

### FIXED CODE ###

#!/usr/bin/env python3

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

    n = 1
    s = 0

    for _ in a:
        if _ == n:
            n += 1
        else:
            s += 1
    if n == 1:
        print(-1)
    else:
        print(s)


main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline

k, q = map(int, input().split())
d_ = list(map(int, input().split()))

for _ in range(q):
    n, x, m = map(int, input().split())
    d = [di%m for di in d_]
    ans = n-1
    
    s, a = divmod(n-1, k)
    ans -= s*d.count(0)
    
    for i in range(a):
        if d[i]==0:
            ans -= 1
            
    last = x+s*sum(d)
    
    for i in range(a):
        last += d[i]
    
    ans -= last//m-x//m
    
    print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

k, q = map(int, input().split())
d_ = list(map(int, input().split()))

for _ in range(q):
    n, x, m = map(int, input().split())
    d = [di%m for di in d_]
    ans = n-1
    zero_acc = [0]
    sum_acc = [0]
    
    for di in d:
        zero_acc.append(zero_acc[-1]+(1 if di==0 else 0))
        sum_acc.append(sum_acc[-1]+di)
    
    s, a = divmod(n-1, k)
    ans -= s*zero_acc[-1]+zero_acc[a]
    last = x+s*sum_acc[-1]+sum_acc[a]
    ans -= last//m-x//m
    
    print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
res = 0
for i in range(N):
  if i+1 in A:
    A = A[A.index(i+1):]
    res += 1
  else:
    break
if res == 0:
  print(-1)
else:
  print(N-res)

### FIXED CODE ###

import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
now = 0
for x in range(N):
  if A[x] == now+1:
    now += 1
if now == 0:
  print(-1)
else:
  print(N-now)
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    inf = 10 ** 16
    h, w = MI()
    aa = LLI(h)
    bb = LLI(h)
    cc = [[a - b for a, b in zip(ar, br)] for ar, br in zip(aa, bb)]
    dp = [[set() for _ in range(w)] for _ in range(h)]
    dp0 = dp[0]
    dp0[0].add(abs(cc[0][0]))
    for j in range(1, w):
        c = cc[0][j]
        for a in dp0[j - 1]:
            dp0[j].add(abs(a + c))
            dp0[j].add(abs(a - c))
    for i in range(1, h):
        c = cc[i][0]
        for a in dp[i - 1][0]:
            dp[i][0].add(abs(a + c))
            dp[i][0].add(abs(a - c))
    for i in range(1, h):
        dpi=dp[i]
        dpi1=dp[i-1]
        cci=cc[i]
        for j in range(1, w):
            c = cci[j]
            for a in dpi1[j]:
                dpi[j].add(abs(a + c))
                dpi[j].add(abs(a - c))
            for a in dpi[j - 1]:
                dpi[j].add(abs(a + c))
                dpi[j].add(abs(a - c))
    print(min(dp[h-1][w-1]))
main()


### FIXED CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def bin_out(aa):
    bb = [format(a, "b") for a in aa]
    print(bb)

def main():
    h, w = MI()
    aa = LLI(h)
    bb = LLI(h)
    max_val=6400
    zero = 1 << max_val
    dp = [0] * w
    dp[0] = zero
    #bin_out(dp)
    for i, (ar, br) in enumerate(zip(aa, bb)):
        for j, (a, b) in enumerate(zip(ar, br)):
            d = abs(a - b)
            if j > 0: dp[j] |= dp[j - 1]
            dp[j] = dp[j] << d | dp[j] >> d
        #bin_out(dp)
    goal = dp[-1]
    for ans in range(max_val + 1):
        if goal & zero << ans or goal & zero >> ans:
            print(ans)
            break

main()
### BUGGY CODE ###

# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
cost_map = [[None]*W for i in range(H)]
# 迷路を最初のマス目の数だけ解く
starts = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            starts.append([x, y])
for sx, sy in starts:
    queue = [[sx, sy]]
    cost_map[sx][sy] = 0
    while queue:
        x, y = queue.pop(0)
        current = cost_map[x][y]
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if cost_map[nx][ny] is None:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
                elif current+1 < cost_map[nx][ny]:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
result = 0
for i in range(H):
    for j in range(W):
        if cost_map[i][j]>result:
            result = cost_map[i][j]
print(result)


### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)
### BUGGY CODE ###

import numpy as np

H,W = list(map(int,input().split()))
INFTY = 10000
A = np.array([[0 if c=="#" else INFTY for c in input()]for _ in range(H)] )

for i in range(H):
    A[:-1] = np.min((A[:-1],A[1:]+1),0)
    A[1:] = np.min((A[1:],A[:-1]+1),0)
A=A.T
for i in range(W):
    A[:-1] = np.min((A[:-1],A[1:]+1),0)
    A[1:] = np.min((A[1:],A[:-1]+1),0)
print(np.max(A))

### FIXED CODE ###

H,W = list(map(int,input().split()))
A = [list(input()) for _ in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            q.append((i,j))
            A[i][j] = 0
ans = 0
k = 0
while k < len(q):
    i,j = q[k]
    v = A[i][j]
    if i>0 and A[i-1][j]==".":
        A[i-1][j] = v + 1
        q.append((i-1,j))
        ans = max(ans,v+1)
    if i<H-1 and A[i+1][j]==".":
        A[i+1][j] = v + 1
        q.append((i+1,j))
        ans = max(ans,v+1)
    if j>0 and A[i][j-1]==".":
        A[i][j-1] = v + 1
        q.append((i,j-1))
        ans = max(ans,v+1)
    if j<W-1 and A[i][j+1]==".":
        A[i][j+1] = v + 1
        q.append((i,j+1))
        ans = max(ans,v+1)
    k += 1
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(M):
  i = A.index(max(A))
  A[i] = A[i]/2
ans = 0
for i in range(N):
  ans += int(A[i])
print(ans)

### FIXED CODE ###

import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
  A[i] = -A[i]
heapq.heapify(A)
for _ in range(M):
  a = -heapq.heappop(A)
  a = (a//2) * -1
  heapq.heappush(A, a)
print(sum(A)*-1)
### BUGGY CODE ###

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

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

### FIXED CODE ###

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

a = max(R, G, B)
if a == R:
  b = max(G, B)
elif a == G:
  b = max(B, R)
else:
  b = max(R, G)
c = min(R, G, B)
for i in range(N//a+1):
  for j in range((N-a*i)//b+1):
    if ((N-a*i)-b*j)%c == 0:
      ans += 1
print(ans)