Harryxun/stf_run huggingface dataset:
from itertools import *
from collections import *
from bisect import *


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

ruiA=[0 for i in range(N+1)]
for i in range(0,N):
    ruiA[i+1]=ruiA[i]+A[i]


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=31
for i in range(len(nlst)):
    a=bin(nlst[i])[2:]
    nlst[i]=[0 for i in range(lenbin-len(a))]+list(map(int,list(a)))
    #print(nlst[i])
lenbinst=0

#print(nlst)
while lenbinst<lenbin:
    idx=-1
    count=0
    for m in range(len(nlst)):
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=lenbinst:
            nlst=[i for i in nlst if i[idx]==1]
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)


### FIXED CODE ###

from itertools import *
from collections import *
from bisect import *
N,K=map(int,input().split())
A=list(map(int,input().split()))

ruiA=[0 for i in range(N+1)]
for i in range(0,N):
    ruiA[i+1]=ruiA[i]+A[i]


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=60
for i in range(len(nlst)):
    lst=[]
    for m in range(lenbin):
        lst.append((nlst[i]>>m)&1)
    nlst[i]=list(reversed(lst))
lenbinst=0

use=set([i for i in range(len(nlst))])
while lenbinst<lenbin:
    idx=-1
    count=0
    dellst=set()
    for m in use:
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=count:
            for m in use:
                if nlst[m][lenbinst]!=1:
                    dellst.add(m)
    use=use-dellst
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]
nlst=[nlst[i] for i in use]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)
s=input()
n=len(s)
s_int=[0]*n
x=pow(10,n-1)
for i in range(n):
    s_int[i]=int(s[i]*x)%2019


### FIXED CODE ###

s=input()
n=len(s)
s_int=[0]*n
x=(pow(10,n-1))%2019
for i in range(n):
    s_int[i]=(int(s[i])*x)%2019
    x=(x*202)%2019
t=[0]*(n+1)
t[0]=int(s)%2019
for i in range(1,n):
    t[i]=t[i-1]-s_int[i-1]
    t[i]%=2019
t.sort()
t_set=sorted(list(set(t)))
tcnt=[0]*len(t_set)
num=0
for i in range(n+1):
    if t_set[num]==t[i]:
        tcnt[num]+=1
    else:
        num+=1
        tcnt[num]+=1
sum=0
for i in range(len(tcnt)):
    sum+=(tcnt[i]*(tcnt[i]-1))//2
print(sum)
S = input()
K = int(input())

if S[0] == '1':
    print(S[1])
else:
    print(S[0])

### FIXED CODE ###

S = input()
K = int(input())

if S[0] == '1':
    counter = 1
    for s in S:
        if s != '1' or counter >= K:
            print(s)
            break
        counter += 1 
else:
    print(S[0])
N = input()
A = 0 if N%1000 == 0 else A = 1000-N%1000
print(A)

### FIXED CODE ###

print((10000-int(input()))%1000)
#coding utf-8
A,B,K= map(int,input().split())
y = list(range(A,B + 1))
for i in y[0:K]:
    print(i)

for i in y[-K-1:-1]:
    print(i)

### FIXED CODE ###

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

if (b-a+1) > 2*k:
    for i in range(a,a+k):
        print(i)
    for i in range(b-k+1,b+1):
        print(i)
else:
    for i in range(a,b+1):
        print(i)
from collections import deque
    
n,p=map(int,input().split())

e=[]
for _ in range(n):
    s,t,x = map(int,input().split())
    e.append((s-x,1,x))
    e.append((t-x,-1,x))
e.sort()
q = deque()
for _ in range(p):
    q.append(int(input()))
s=set()
#print(e)
for t,f,x in e:
    while t > q[0]:
        #print((t,f,x,q.popleft()))
        q.popleft()
        if len(s) == 0:
            print(-1)
        else:
            print(min(s))
    if f == 1:
        s.add(x)
    else:
        s.remove(x)
while q:
    q.pop()
    print(-1)

### FIXED CODE ###

import sys
input = sys.stdin.readline
 
n,q = map(int,input().split())
imos = []
for i in range(n):
    s,t,x = map(int,input().split())
    imos.append((s-x,x))
    imos.append((t-x,-x))
ans = set()
j = 0
for i in range(q):
    d = int(input())
    imos.append((d,1e100))
imos.sort()
m = 1e100
flag = False
for t,stop in imos:
    if stop == 1e100:
        if ans:
            if not flag:
                m = min(ans)
                flag = True
            print(m)
        else:
            print(-1)
    elif stop > 0:
        ans.add(stop)
        if stop < m:
            m = stop
            flag = True
    else:
        ans.remove(-stop)
        if m == -stop:flag = False
N, K = map(int, input().split())
dic = {}
ans = []
uniq = []
etc = []
def calc_ans(ans):
    x = 0
    tmp = 0
    for i in range(K):
      tmp += ans[i][0]
      x += ans[i][1]
    return tmp + (x * x)

for i in range(N):
    l = list(map(int, input().split()))
    if dic.get(l[0]):
        dic[l[0]].append(l[1])
    else:
        dic[l[0]] = [l[1]]
if N <= K:
    ans = 0
    for i in dic.keys():
        ans += sum(dic[i])
    ans += len(dic.keys()) * len(dic.keys())
    print(ans)
    exit()
for t in dic.keys():
    dic[t].sort(reverse=True)
    uniq.append((dic[t][0], t))
uniq.sort(reverse=True)
for i in range(K):
  ans.append((uniq[i][0], 1))
  for n in dic[uniq[i][1]][1:]:
      if n <= uniq[K-1][0]:
          break
      etc.append((n, 0))
ans_res = calc_ans(ans)
all_ans = ans_res
for n in etc:
    ans.sort(reverse=True)
    ans[-1] = n
    tmp_res = calc_ans(ans)
    if all_ans < tmp_res:
        all_ans = tmp_res
    ans_res = tmp_res
print(all_ans)

### FIXED CODE ###

N,K = map(int,input().split())
td = [[int(i) for i in input().split()] for _ in range(N)]
td.sort(key=lambda x:x[1],reverse=True)
ans = 0
ls = set()
dl = []
for i in range(K):
    ans += td[i][1]
    if not td[i][0] in ls:
        ls.add(td[i][0])
    else:
        dl.append(td[i][1])
ans = ans + len(ls)**2
res = ans
for i in range(K,N):
    if not dl:
        break
    if td[i][0] in ls:
        continue
    ans += len(ls)*2+1
    ans += td[i][1]
    ans -= dl[-1]
    del dl[-1]
    res = max(res,ans)
    ls.add(td[i][0])
print(res)
K = int(input())
k = K
A,B = map(int, input().split())
for i in range(1, B+1):
    k *= i
    if k > A and k < B:
        print('OK')
        a = True
        break
    else:
        k = K
        continue
if a != True:
    print('NG')

### FIXED CODE ###

K = int(input())
k = K
A,B = map(int, input().split())
for i in range(1, B+1):
    k *= i
    if k >= A and k <= B:
        print('OK')
        a = True
        break
    else:
        k = K
        a = False
        continue
if a != True:
    print('NG')
s=set(input().split())
a=set('abcdefghijklmnopqrstuvwxyz')
a.split()
if s==a:
    print('None')
else:
    print(s-a)


### FIXED CODE ###

s=set(list(input()))
a=set(list('abcdefghijklmnopqrstuvwxyz'))

if s==a:
    print('None')
else:
    b=sorted(list(a-s))
    print(str(b[0]))
if N % 4 == 0 or N % 7 == 0 or N % 11 == 0 or N % 4 == 7 or N % 7 == 4 or N % 11 == 7 or N % 11 == 4:
    print('Yes')   
else :
    print('No')

### FIXED CODE ###

N =int(input())

for i in range(0, 100//7):
    for j in range(0, 100//4):
        if 7*i + 4*j == N:
            print('Yes')
            exit()

print('No')
import sys
from bisect import bisect_left,bisect_right,insort
input = sys.stdin.readline

def main():
    n=int(input())
    s=list(input())
    p=[[] for _ in range(26)]
    for i in range(n):
        p[ord(s[i])-ord('a')].append(i+1)
    q=int(input())
    for i in range(q):
        a,b,c=map(str,input().split())
        if a=='1':
            b=int(b)-1
            if s[b]==c:
                continue
            else:
                insl=bisect_left(p[ord(c)-ord('a')],b)
                p[ord(s[i])-ord('a')].pop(insl)
                insort(p[ord(c)-ord('a')],insl)
                s[b]=c
        else:
            b,c=int(b)-1,int(c)-1
            cnt=0
            for i in range(26):
                insl=bisect_left(p[i],b)
                if insl>=len(p[i]):
                    continue
                elif p[i][insl]<=c:
                    cnt+=1
            print(cnt)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
from bisect import bisect_left,bisect_right,insort
#input = sys.stdin.readline

def main():
    n=int(input())
    s=list(str(input()))
    p=[[] for _ in range(26)]
    for i,S in enumerate(s):
        p[ord(S)-ord('a')].append(i)
    q=int(input())
    for i in range(q):
        a,b,c=map(str,input().split())
        if a=='1':
            b=int(b)-1
            if s[b]==c:
                continue
            else:
                insl=bisect_left(p[ord(s[b])-ord('a')],b)
                p[ord(s[b])-ord('a')].pop(insl)
                insort(p[ord(c)-ord('a')],b)
                s[b]=c
        else:
            b,c=int(b)-1,int(c)-1
            cnt=0
            for i in range(26):
                insl=bisect_left(p[i],b)
                if insl>=len(p[i]):
                    continue
                elif p[i][insl]<=c:
                    cnt+=1
            print(cnt)

if __name__ == '__main__':
    main()
x,y = map(int, raw_input().split(' '))


"""
a + b = x
2a + 4b = y

a = x-b
2x- 2b + 4b = y

a = x-b
2b = y  - 2x
"""
def f(x,y):
  
	if (y - 2*x) < 0 or (y - 2*x) %2 != 0:
      return False
  	b = y - 2*x
    a = x - b
    if a < 0:
      return False
    return True
  
print 'Yes' if f(x,y) else 'No'

### FIXED CODE ###

x,y = map(int, raw_input().split(' '))


"""
a + b = x
2a + 4b = y

a = x-b
2x- 2b + 4b = y

a = x-b
2b = y  - 2x
"""
def f(x,y):
  	
	if (y - 2*x) < 0: return False
	b = (y - 2*x)/2
	a = x - b

	b = (y - 2*x)/2
	a = x - b
	
	if a < 0: return False


	return a+b == x and 2*a + 4*b ==y
  
print 'Yes' if f(x,y) else 'No'
# coding: utf-8
# Your code here!
import sys
def input():
    return sys.stdin.readline()
n,m=map(int,input().split())
a=[0]*(n+1)
b=[0]*(n+1)
c=[0]*(n+1)
f1=[]
f2=[]
for i in range(m):
    a[i],b[i]=map(int,input().split())
    if a[i]==1:
        c[b[i]]=1

ans='IMPOSSIBLE'
for i in range(m):
    if b[i]==n:
        if c[a[i]]==1:
            ans='POSSIBLE'
print(ans)


### FIXED CODE ###

# coding: utf-8
# Your code here!
import sys
def input():
    return sys.stdin.readline()
n,m=map(int,input().split())
a=[0]*(m+1)
b=[0]*(m+1)
c=[0]*(n+1)
f1=[]
f2=[]
for i in range(m):
    a[i],b[i]=map(int,input().split())
    if a[i]==1:
        c[b[i]]=1

ans='IMPOSSIBLE'
for i in range(m):
    if b[i]==n:
        if c[a[i]]==1:
            ans='POSSIBLE'
print(ans)
import math
a,b,c,d = map(int,input().split())
e = c*d//math.gcd(c,d)
n = b-a+1
nc = (n+a%c)//c
nd = (n+a%d)//d
ne = (n+a%e)//e
print(n-(nc+nd-ne))

### FIXED CODE ###

import fractions
a,b,c,d = map(int,input().split())
e = c*d//fractions.gcd(c,d)
cnt = b-a+1
c_cnt = b//c-(a-1)//c
d_cnt = b//d-(a-1)//d
e_cnt = b//e-(a-1)//e
print(cnt-c_cnt-d_cnt+e_cnt)
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = input()

ans = 0
tmp = 0
for i in range(1, N):
	if S[i] == "#" and S[i-1] == ".":
		w = 0
		flg = False
		for x in S[tmp:i]:
			if x == "#":
				flg = True
			if flg and x == ".":
				w += 1

		b = 0
		flg = False
		for x in S[tmp:i][::-1]:
			if x == ".":
				flg = True
			if flg and x == "#":
				b += 1

		ans += min(w, b)
		tmp = i
		
w = 0
flg = False
for x in S[tmp:]:
	if x == "#":
		flg = True
	if flg and x == ".":
		w += 1

b = 0
flg = False
for x in S[tmp:][::-1]:
	if x == ".":
		flg = True
	if flg and x == "#":
		b += 1

ans += min(w, b)
print(ans)

### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

A, B, C = MAP()

if C == sorted([A, B, C])[1]:
	print("Yes")
else:
	print("No")
S = str(input())
T = str(input())
t = int(len(T)) - 2

if S == T[:t]
  print('Yes')
else:
  print('No')

### FIXED CODE ###

S = str(input())
T = str(input())
t = int(len(T)) - 1
U = T[:t]

if S == U:
  print('Yes')
else:
  print('No')
X = input()
Y = len(X)
if (Y == 3):
  print("Yes")
else
  print("No")

### FIXED CODE ###

X = input()
Y = X.count("A")
Z = X.count("B")
if (Y == 3 or Z == 3):
  print("No")
else:
  print("Yes")
import sys
import heapq
import copy
import numpy as np
from collections import deque

h,w=list(map(int,input().split()))
s=np.array([[x for x in input()] for i in range(h)])
d=[[0]*w for i in range(h)]

q=deque([[1,0,0]])

while True:
    cnt,hh,ww=deque.popleft(q)

    if hh==h-1 and ww==w-1:
        nblack=np.sum(s=="#",dtype=np.int)
        print(h*w-nblack-cnt)
        exit(0)
    for nh,nw in [[hh+dh,ww+dw] for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]]:
        if 0>nh or nh>=h or 0>nw or nw>=w or d[nh][nw]==1 or s[nh][nw]=='#':
            continue
        d[nh][nw]=1
        deque.append(q,[cnt+1,nh,nw])

### FIXED CODE ###

import sys
import heapq
import copy
import numpy as np
from collections import deque

h,w=list(map(int,input().split()))
s=np.array([[x for x in input()] for i in range(h)])
d=[[0]*w for i in range(h)]

q=deque([[1,0,0]])

while len(q)>0:
    cnt,hh,ww=deque.popleft(q)

    if hh==h-1 and ww==w-1:
        nblack=np.sum(s=="#",dtype=np.int)
        print(h*w-nblack-cnt)
        exit(0)
    for nh,nw in [[hh+dh,ww+dw] for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]]:
        if 0>nh or nh>=h or 0>nw or nw>=w or d[nh][nw]==1 or s[nh][nw]=='#':
            continue
        d[nh][nw]=1
        deque.append(q,[cnt+1,nh,nw])

print(-1)
n = int(input())
count = 0
flag = False
while count < n:
  a, b = map(int, input().split())
  count += 1
  if a == b and count <= n:
    a, b = map(int, input().split())
    count += 1
    if a == b and count <= n:
      a, b = map(int, input().split())
      count += 1
      if a == b and count <= n:
        flag = True
        print("Yes")
        break

if flag == False:
  print("No")

### FIXED CODE ###

n = int(input())
count = 0
flag = False
while count < n:
  a, b = map(int, input().split())
  count += 1
  if a == b and count < n:
    a, b = map(int, input().split())
    count += 1
    if a == b and count < n:
      a, b = map(int, input().split())
      count += 1
      if a == b and count <= n:
        flag = True
        print("Yes")
        break

if flag == False:
  print("No")
import copy
N = int(input())
A = list(map(int,input().split()))
A.sort()
B = copy.copy(A)
cnt = 0
for i in range(N):
    if A.count(A[i])> 1:
        continue
    for j in range(len(B)):
        if 2 * B[j] > A[i]:
            cnt += 1
            break
        if A[i] % B[j] == 0:
            break
        if j
print(cnt)

### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    MAX = A[-1]
    dp = [1] * (MAX + 5)
    ANS = 0
    for i in range(N):
        if dp[A[i]] == 0:
            continue
        for j in range(MAX+1):
            if A[i]*j > MAX:
                break
            dp[A[i]*j] = 0
        dp[A[i]] = 0
        if i < N-1 and A[i] == A[i+1]:
            continue
        ANS += 1
    print(ANS)

if __name__ == '__main__':
    main()
from collections import deque
readline = sys.stdin.readline

K = int(readline())

inf = 10**9+7
dist = [inf]*(K+1)
dist[K] = 0
Q = deque([K])
used = [False]*(K+1)
while Q:
    vn = Q.pop()
    if used[vn]:
        continue
    used[vn] = True
    vf = (vn*10)%K
    if vn%K and not used[vf]:
        dist[vf] = min(dist[vf], dist[vn])
        Q.append(vf)
    vf = (vn+1)%K
    if not used[(vn+1)%K]:
        dist[vf] = min(dist[vf], 1+dist[vn])
        Q.appendleft(vf)
print(dist[0])        


### FIXED CODE ###

import sys
from collections import deque
readline = sys.stdin.readline

K = int(readline())

inf = 10**9+7
dist = [inf]*(K+1)
dist[K] = 0
Q = deque([K])
used = [False]*(K+1)
while Q:
    vn = Q.pop()
    if used[vn]:
        continue
    used[vn] = True
    vf = (vn*10)%K
    if vn%K and not used[vf]:
        dist[vf] = min(dist[vf], dist[vn])
        Q.append(vf)
    vf = (vn+1)%K
    if not used[(vn+1)%K]:
        dist[vf] = min(dist[vf], 1+dist[vn])
        Q.appendleft(vf)
print(dist[0])
a,b,c,d = map(int,input(),split())
if abs(a-b) <= d and abs(b-c) <= d:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

a,b,c,d = map(int,input().split())
if (abs(a-b) <= d and abs(b-c) <= d) or abs(a-c) <= d:
  print("Yes")
else:
  print("No")
n=int(input())
a=[]
for i in range(n):
    x,y,h=map(int,input().split())
    a.append([x,y,h])

def hight(inp):
    cx,cy,x,y,h=inp
    if h==0:
        return -1
    else:
        return abs(x-cx)+abs(y-cy)+h
    
for cx in range(101):
    for cy in range(101):
        H=set()
        for i in range(n):
            h=hight([cx,cy]+a[i])
            if h!=-1:
                H.add(h)
        if len(H)==1:
            ans=cx,cy,*H
print(*ans)

### FIXED CODE ###

n=int(input())
a=[]
for i in range(n):
    x,y,h=map(int,input().split())
    a.append([x,y,h])

def hight(inp):
    cx,cy,x,y,h=inp
    if h==0:
        return -1
    else:
        return abs(x-cx)+abs(y-cy)+h
    
for cx in range(101):
    for cy in range(101):
        H=set()
        for i in range(n):
            h=hight([cx,cy]+a[i])
            if h!=-1:
                H.add(h)
        if len(H)==1:
            flg=True
            for i in H:
                d=i
            for i in range(n):
                x,y,h=a[i]
                if max(0,d-abs(x-cx)-abs(y-cy))!=h:
                    flg=False
            if flg:
                ans=cx,cy,d
print(*ans)
N , M ,C  = map(int,input().split())
B = map(int, input().split())

count = 0
for i in range(N):
    A = list(map(int, input().split()))
    p = C
    for j in range(M):
        p+= A[j]*B[j]
        if p > 0:
            count+=1
print(count)



### FIXED CODE ###

N,M,C=map(int,input().split())
B=list(map(int,input().split()))
count=0
for i in range(N):
 A=list(map(int,input().split()))
 p=C
 for j in range(M):p+=A[j]*B[j]
 if p>0:count+=1
print(count)
import sys

def process(dataset):
    data = []
    for line in dataset:
        line = line.split()
        if len(line) == 2:
            if line[0] == line[1] == "0":
                data = []
                continue
            H = int(line[0])
            W = int(line[1])
        elif len(line) == 0:
            pass
        else:
            data.append(line[0])
            if len(data) == H:
                field = create_field(H, W)
                print search(H, W, data, field)

def search(H, W, data, field):
    count = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 0:
                continue
            _search(i, j, H, W, data[i][j], data, field)
            count += 1
    return count

def _search(i, j, H, W, cell, data, field):
    field[i][j] = 0
    for a in xrange(i-1, i+2):
        if -1 < a < H:
            pass
        else:
            continue
        for b in xrange(j-1, j+2):
            if -1 < b < W:
                pass
            else:
                continue

            if a != i and b != j:
                continue

            if field[a][b] == 0:
                continue
            if cell == data[a][b]:
                _search(a, b, H, W, cell, data, field)
    return

def create_field(H, W):
    field = []
    for i in xrange(H):
        raw = []
        for j in xrange(W):
            raw.append(1)
        field.append(raw)
    return field

if __name__ == "__main__":
    dataset = [line.strip() for line in sys.stdin.readlines()]
    process(dataset)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000000)


def main():
    solve()


def solve():
    for data in get_dataset():
        print search(data)


def search(data):
    count = 0
    H = len(data)
    W = len(data[0])
    for i in xrange(H):
        for j in xrange(W):
            if data[i][j] == '.':
                continue
            _search(i, j, H, W, data[i][j], data)
            count += 1
    return count


def _search(i, j, H, W, cell, data):
    data[i][j] = '.'
    for a in xrange(i-1, i+2):
        if not -1 < a < H:
            continue
        for b in xrange(j-1, j+2):
            if not -1 < b < W:
                continue

            if a != i and b != j:
                continue

            if data[a][b] == '.':
                continue

            if cell == data[a][b]:
                _search(a, b, H, W, cell, data)
    return


def get_dataset():
    dataset = []
    while True:
        line = raw_input().rstrip().split()
        H = int(line[0])
        if H == 0:
            break

        data = []
        for i in xrange(H):
            data.append(list(raw_input().rstrip()))
        dataset.append(data)
    return dataset


if __name__ == '__main__':
    main()
import sys
n,m=map(int,input().split())
tmp=[-1]*n
for _ in range(m):
    s,c=map(int,input().split())
    s-=1
    if tmp[s]<0:
        tmp[s]=c
    if tmp[s]!=c:
        print(-1)
        sys.exit()
if tmp[0]==0 and n>1:
    print(-1)
    sys.exit()
if tmp[0]<0 and n>1:
    tmp[0]=1
if n==1 and tmp[0]==-1:
    tmp[0]=0
for i in range(1,n):
    if tmp[i]<0:
        tmp[i]=0
t=''.join(tmp)
print(t)

### FIXED CODE ###

import sys
n,m=map(int,input().split())
tmp=[-1]*n
for _ in range(m):
    s,c=map(int,input().split())
    s-=1
    if tmp[s]<0:
        tmp[s]=c
    if tmp[s]!=c:
        print(-1)
        sys.exit()
if tmp[0]==0 and n>1:
    print(-1)
    sys.exit()
if tmp[0]<0 and n>1:
    tmp[0]=1
if n==1 and tmp[0]==-1:
    tmp[0]=0
for i in range(1,n):
    if tmp[i]<0:
        tmp[i]=0
for i in tmp:
    print(i,end='')
N = int(input())

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

Sum = list(0,abs(cost[0]-cost[1]))

for i in range(2,N):
  Sum[i] = min(Sum[i-1] + abs(cost[i]-cost[i-1]), Sum[i-2] + abs(Sum[i-2] - Sum[i]))
  
print(Sum[N-1])

### FIXED CODE ###

N = int(input())

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

dp = [0]*N

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

for i in range(2,N):
  dp[i] = min (dp[i-1] + abs(cost[i] - cost[i-1]), dp[i-2] + abs(cost[i-2] - cost[i]))
  
print(dp[-1])