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

n = int(input())
s = []
for _ in range(n):
    s.append(input())

num_char = [[0 for _ in range(26)] for _ in range(n)]
for i in range(n):
    for c in s[i]:
        num_char[i][ord(c) - 97] += 1

count = 0
for i in range(n):
    for j in range(i+1, n):
        if num_char[i] == num_char[j]:
            count += 1

print(count)


### FIXED CODE ###

n = int(input())
s_list = []
for _ in range(n):
    s_list.append(''.join(sorted(input())))

s_list = sorted(s_list)

count = 0
total = 0
s_prev = s_list[0]
for s in s_list[1:]:
    if s_prev == s:
        count += 1
    else:
        if count > 0:
            total += count * (count + 1) // 2
            count = 0
    s_prev = s

if count > 0:
    total += count * (count + 1) // 2

print(total)
### BUGGY CODE ###

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

c = 1
for i in range(n):
  if(i != 0 and sum(map(lambda x: p[i] <= x, p[:i])) == i):
    c += 1  
print(c)

### FIXED CODE ###

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

MIN = 10e5
c = 0
for i in range(n):
  if p[i] <= MIN:
    MIN = p[i]
    c += 1
print(c)
### BUGGY CODE ###

import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    for c in range(1, k+1):
      total += math.gcd(a,math.gcd(b,c))
      
print(total)

### FIXED CODE ###

import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    if math.gcd(a,b) == 1:
      total += k
    else:
      for c in range(1, k+1):
        total += math.gcd(a,math.gcd(b,c))
    
print(total)
### BUGGY CODE ###

K,S = list(map(int,input().split()))
counter = 0
counter_d = 0
counter_tri =0
for i in range(K+1):
    for j in range(i+1,K+1):
        for k in range(j+1,K+1):
            if i+j+k ==S:
                counter +=6
for i in range(K+1):
    for k in range(K+1):
       if 2*i+k== S:
           if i == k:
               counter += 1
           else:
               counter +=3
print(counter)


### FIXED CODE ###

K,S = list(map(int,input().split()))
counter = 0
for i in range(K+1):
    for j in range(K+1):
            if 0<=S-i-j<=K:
                counter +=1
               # print(str(i)+' '+str(j)+ ' ' + str(S-i-j))
print(counter)
### BUGGY CODE ###

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

cnt = 0
for i in range(N):
  if all([A[v] >= A[i] for v in range(i)]): 
    cnt = cnt +1

print(cnt)

### FIXED CODE ###

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

min = 0
cnt = 0
for i in range(N):
  if i == 0:
    cnt += 1
    min = A[i]
    continue
  if A[i] <= min:
    cnt += 1
    min = A[i]

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

from bisect import bisect_right
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))

count = 0
for i in range(N):
    b = bisect_right(B,A[i])
    for j in range(b,N):
        count += N - bisect_right(C,B[j])
print(count)

### FIXED CODE ###

import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
count = 0
for j in range(N):
    c = N-bisect.bisect_right(C,B[j])
    b = bisect.bisect_right(A,B[j]-1)
    count += b*c
print(count)
### BUGGY CODE ###

from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = []
for i in range(H):
    c.append(input().rstrip())

d = deque([(0, x1-1, y1-1)])

dp = [[False] * W for _ in range(H)]


def check(step, x, y):
    if c[x][y] == '@':
        return False

    if not dp[x][y]:
        dp[x][y] = True
        d.append((step + 1, x, y))

    return True


while d:
    step, x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for i in range(x+1, min(x+K+1, H)):
        if not check(step, i, y):
            break

    for i in range(x-1, max(-1, x-K-1), -1):
        if not check(step, i, y):
            break

    for j in range(y+1, min(y+K+1, W)):
        if not check(step, x, j):
            break

    for j in range(y-1, max(-1, y-K-1), -1):
        if not check(step, x, j):
            break
else:
    print(-1)


### FIXED CODE ###

from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
def main():
    n = int(input())
    A = list(map(int,input().split()))

    dp = [[-float("INF")]*(n+1) for i in range(n+1)]
    low = [-float("INF")]*(n+1)
    col = [-float("INF")]*(n+1)
    m = 0
    a,b = A[:2]
    dp[a][b] = 0
    dp[b][a] = 0
    low[a] = 0
    col[a] = 0
    low[b]
    col[b] = 0
    base = 0

    for _ in range(1,n):
        a,b,c = A[3*_-1:3*_+2]
        q = []
        if a==b==c:
            base += 1
            continue
        for j in range(3):
            q.append((a,b,max(m,dp[c][c]+1)))
            for i in range(1,n+1):
                q.append((a,i,low[i]))
            if a==b:
                for i in range(1,n+1):
                    q.append((c,i,dp[a][i]+1))
            a,b,c = b,c,a
        for a,b,v in q:
            dp[a][b] = max(dp[a][b],v)
            dp[b][a] = max(dp[b][a],v)
            low[a] = max(low[a],v)  
            low[b] = max(low[b],v)  
            col[a] = max(col[a],v)  
            col[b] = max(col[b],v) 
            m = max(m,v)     
    print(max(m,dp[A[-1]][A[-1]]+1)+base)
if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

dp = [[-float("INF")]*(n+1) for i in range(n+1)]
low = [-float("INF")]*(n+1)
m = 0
a,b = A[:2]
dp[a][b] = 0
dp[b][a] = 0
low[a] = 0
low[b] = 0
base = 0

for _ in range(1,n):
    a,b,c = A[3*_-1:3*_+2]
    q = []
    if a==b==c:
        base += 1
        continue
    m1 = max(m,max(dp[a][a],dp[b][b],dp[c][c])+1)
    for j in range(3):
        q.append((a,b,max(m,dp[c][c]+1)))
        for i in range(1,n+1):
            q.append((a,i,low[i]))
        if a==b:
            for i in range(1,n+1):
                q.append((c,i,dp[a][i]+1))
                m1 = max(m1,dp[a][i]+1)
        a,b,c = b,c,a
    for a,b,v in q:
        dp[a][b] = max(dp[a][b],v)
        dp[b][a] = max(dp[b][a],v)
        low[a] = max(low[a],v)  
        low[b] = max(low[b],v)      
    m = m1
print(max(m,dp[A[-1]][A[-1]]+1)+base)
### BUGGY CODE ###

n = int(input())
order = list(map(int, input().split()))
while (min(order) != 999999):
  minVal = min(order)
  minIndex = order.index(minVal)
  print(minIndex+1, end = ' ')
  order[minIndex] = 999999

### FIXED CODE ###

n = int(input())
order = list(map(int, input().split()))
correctOrder = [0 for x in range(n)]
for i in range(n):
    correctOrder[order[i]-1] = str(i+1)
toStr = ' '.join(correctOrder)
print(toStr)
### BUGGY CODE ###

import math as ma
import sys
from sys import exit
from decimal import Decimal as dec
from itertools import permutations


def li():
	return list(map(int , input().split()))


def num():
	return map(int , input().split())


def nu():
	return int(input())


def find_gcd(x , y):
	while (y):
		x , y = y , x % y
	return x


s=input()
k=nu()
n=len(s)
z=set()
for i in range(n):
	pp=""
	for j in range(i,n):
		pp+=s[j]
		z.add(pp)
fin=list(z)
fin.sort()
print(fin[k-1])

### FIXED CODE ###

import math as ma
import sys
from sys import exit
from decimal import Decimal as dec
from itertools import permutations


def li():
	return list(map(int , input().split()))


def num():
	return map(int , input().split())


def nu():
	return int(input())


def find_gcd(x , y):
	while (y):
		x , y = y , x % y
	return x


s=input()
k=nu()
n=len(s)
m=k
z=set()
for i in range(n):
	pp=""
	for j in range(i,i+m):
		if(j>=n):
			break
		pp+=s[j]
		z.add(pp)
fin=list(z)
fin.sort()
print(fin[k-1])
### BUGGY CODE ###

import collections
s = input()
u = collections.deque([0])
for i in range(len(s)):
    u.append((u[i]+int(s[::-1][i])*10**(i%224))%2019)
u2 = collections.Counter(u)
sum = 0
for i in u2.values():
    sum += i*(i-1)/2
print(int(sum))

### FIXED CODE ###

import collections
s = input()
u = collections.deque([0])
for i in range(len(s)):
    u.append((u[i]+int(s[-i-1])*10**(i%224))%2019)
u2 = collections.Counter(u)
sum = 0
for i in u2.values():
    sum += i*(i-1)/2
print(int(sum))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
mod = 998244353
N, S = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0
num = [0] * 6020
for i in range(N):
    for j in range(3010, 0, -1):
        num[A[i]+j] += num[j]
        num[A[i]+j] %= mod
    num[A[i]] += i+1
    num[A[i]] %= mod
    ans += num[S] * (N - i)
    ans %= mod
    num[S] = 0
print(ans)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
mod = 998244353
N, S = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0
num = [0] * 6020
for i in range(N):
    for j in range(3010, 0, -1):
        num[A[i]+j] += num[j]
        num[A[i]+j] %= mod
    num[A[i]] += i+1
    num[A[i]] %= mod
    ans += num[S] * (N - i)
    ans %= mod
    num[S] = 0
print(ans)
### BUGGY CODE ###

while True:
    [W, H] = [int(x) for x in raw_input().split()]
    if [W, H] == [0, 0]:
        break
    print('#' * W)
    counter = 0
    while counter < H - 1:
        print('#' + '.' * (W - 2) + '#')
    print('#' * W)

### FIXED CODE ###

while True:
    [H, W] = [int(x) for x in raw_input().split()]
    if [H, W] == [0, 0]:
        break
    print('#' * W)
    counter = 0
    while counter < H - 2:
        print('#' + '.' * (W - 2) + '#')
        counter += 1
    print('#' * W + '\n')
### BUGGY CODE ###

from collections import defaultdict

N, Q = map(int, input().split())
edge = defaultdict(list)
for i in range(N-1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)

cnt = [0]*(N+1)
for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

root_id = 1
open_list = [root_id]
closed_list = []
while True:
    if len(open_list) == 0:
        break
    cur_id = open_list.pop()
    closed_list.append(cur_id)
    adj_id_list = list(set(edge[cur_id]) - set(closed_list))
    for adj_id in adj_id_list:
        cnt[adj_id]+=cnt[cur_id]
        open_list.append(adj_id)
print(*cnt[1:])

### FIXED CODE ###

from collections import defaultdict

N, Q = map(int, input().split())
edge = defaultdict(list)
for i in range(N-1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)

cnt = [0]*(N+1)
for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

root_node = (1, -1) # (id, value, parent_id)
open_list = [root_node]
closed_list = []
while True:
    if len(open_list) == 0:
        break
    cur_node = open_list.pop()
    cur_id, parent_id = cur_node
    if not(parent_id == -1):
        cnt[cur_id] += cnt[parent_id]
    for child_id in edge[cur_id]:
        if child_id == parent_id:
            continue
        open_list.append((child_id, cur_id))
print(*cnt[1:])
### BUGGY CODE ###

from collections import defaultdict
from functools import lru_cache
import numpy as np


def trial_division(n):
    factor = defaultdict(int)
    tmp = int(np.sqrt(n)) + 1
    for num in range(2, tmp):
        while n % num == 0:
            n //= num
            factor[num] += 1
    factor[n] += 1
    return factor


@lru_cache(maxsize=None)
def powmod(x, y, mod):
    if y == 0:
        return 1
    return (((powmod(x, y >> 1, mod) ** 2) % mod) * x ** (y & 1)) % mod


n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
numbers = defaultdict(int)
for a_i in a:
    for i, v in trial_division(a_i).items():
        numbers[i] = max(v, numbers[i])

l = 1
for i, v in numbers.items():
    l *= powmod(i, v, mod)
    l %= mod


ans = 0
for i in a:
    r = powmod(i, mod-2, mod)
    ans += l * r
    ans %= mod
print(int(ans))




### FIXED CODE ###

from collections import defaultdict
from functools import lru_cache
import math


def trial_division(n):
    factor = defaultdict(int)
    tmp = int(math.sqrt(n)) + 1
    for num in range(2, tmp):
        while n % num == 0:
            n //= num
            factor[num] += 1
    factor[n] += 1
    return factor


@lru_cache(maxsize=None)
def powmod(x, y, mod):
    if y == 0:
        return 1
    return (((powmod(x, y >> 1, mod) ** 2) % mod) * x ** (y & 1)) % mod


n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
numbers = defaultdict(int)
for a_i in a:
    for i, v in trial_division(a_i).items():
        numbers[i] = max(v, numbers[i])

l = 1
for i, v in numbers.items():
    l *= powmod(i, v, mod)
    l %= mod


ans = 0
for i in a:
    r = powmod(i, mod-2, mod)
    ans += l * r
    ans %= mod
print(int(ans))
### BUGGY CODE ###

import itertools
import copy

N = int(input())
S = list(input())

ans = set()
count = 0
for i in itertools.combinations(range(len(S)), len(S)-3):
  count = 0
  tmp = copy.copy(S)
  for j in i:
    tmp.pop(j-count)
    count+=1
  ans.add(str(tmp))
print(len(ans))



### FIXED CODE ###

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

count = 0
for i in range(1000):
  tmp = format(i, '03')
  k = 0
  for j in s:
    if j == tmp[k]:
      k+=1
    if k == 3:
      count+=1
      break
print(count)
### BUGGY CODE ###

N, K = map(int, raw_input().split())
ps = []
for i in xrange(N):
  ps.append(map(int, raw_input().split()))

xsorted = zip(*sorted(ps, key=lambda p: p[0]))[0]
ysorted = zip(*sorted(ps, key=lambda p: p[1]))[1]

ans = float('inf')
for i in xrange(N):
  bottom = ysorted[i]
  for j in xrange(i+K-1, N):
    top = ysorted[j]
    answered = False
    for k in xrange(N):
      right = xsorted[k]
      for l in xrange(k+K-1, N):
        left = xsorted[l]
        c = 0
        for a in xrange(N):
          if right <= ps[a][0] <= left and bottom <= ps[a][1] <= top:
            c += 1
            if c >= K: break
        else:
          continue
        area = (left - right) * (top - bottom)
        if area < ans:
          ans = area
        break
      else:
        continue
      answered = True

print ans

### FIXED CODE ###

N, K = map(int, raw_input().split())
ps = []
for i in xrange(N):
  ps.append(map(int, raw_input().split()))

xsorted = zip(*sorted(ps, key=lambda p: p[0]))[0]
ysorted = zip(*sorted(ps, key=lambda p: p[1]))[1]

ans = float('inf')
for i in xrange(N):
  bottom = ysorted[i]
  for j in xrange(i+K-1, N):
    top = ysorted[j]
    for k in xrange(N):
      if k+K-1 >= N:
        break
      right = xsorted[k]
      # 2 bun tansaku
      f, t = k+K-1, N
      sol = None
      while True:
        mid = (f+t)//2
        left = xsorted[mid]
        c = 0
        for p in ps:
          if right <= p[0] <= left and bottom <= p[1] <= top:
            c += 1
            if c >= K: break
        else:
          if f+1 >= t:
            break
          f = mid
          continue
        sol = mid
        if f+1 >= t:
          break
        t = mid
      if sol:
        area = (xsorted[sol] - right) * (top - bottom)
        if area < ans:
          ans = area

print ans
### BUGGY CODE ###

import random


class Dice(object):
    def __init__(self):
        self.t = 1
        self.s = 2
        self.e = 3
        self.w = 4
        self.n = 5
        self.b = 6

    def __init__(self, faces):
        self.t = faces[0]
        self.s = faces[1]
        self.e = faces[2]
        self.w = faces[3]
        self.n = faces[4]
        self.b = faces[5]

    def roll(self, direct):
        if direct == 0:
            self.t, self.s, self.b, self.n = self.n, self.t, self.s, self.b
        elif direct == 1:
            self.t, self.w, self.b, self.e = self.e, self.t, self.w, self.b
        elif direct == 2:
            self.n, self.w, self.s, self.e = self.w, self.s, self.e, self.n

    def is_equal(self, dice):
        if self.t == dice.t and self.s == dice.s and self.e == dice.e and self.w == dice.w and self.n == dice.n and self.b == dice.b:
            return True
        else:
            return False

n = int(input())
dice_list = []
flag_list = []
for _ in range(n):
    flag = False
    faces = list(map(int, input().split()))
    dice_list.append(Dice(faces))

    if n == 0:
        continue

    dice_a = dice_list[-1]
    for i in range(len(dice_list) - 1):
        dice_b = dice_list[i]

        for i in range(1000):
            dice_b.roll(random.randint(0, 3))
            if dice_a.is_equal(dice_b):
                flag = True
                break

        flag_list.append(flag)

if any(flag_list):
    print('No')
else:
    print('Yes')



### FIXED CODE ###

import random


class Dice(object):
    def __init__(self):
        self.t = 1
        self.s = 2
        self.e = 3
        self.w = 4
        self.n = 5
        self.b = 6

    def __init__(self, faces):
        self.t = faces[0]
        self.s = faces[1]
        self.e = faces[2]
        self.w = faces[3]
        self.n = faces[4]
        self.b = faces[5]

    def roll(self, direct):
        if direct == 0:
            self.t, self.s, self.b, self.n = self.n, self.t, self.s, self.b
        elif direct == 1:
            self.t, self.w, self.b, self.e = self.e, self.t, self.w, self.b
        elif direct == 2:
            self.n, self.w, self.s, self.e = self.w, self.s, self.e, self.n

    def is_equal(self, dice):
        if self.t == dice.t and self.s == dice.s and self.e == dice.e and self.w == dice.w and self.n == dice.n and self.b == dice.b:
            return True
        else:
            return False

    def get_nums(self):
        return {self.t, self.s, self.e, self.w, self.n, self.b}

n = int(input())
dice_list = []
flag_list = []
for _ in range(n):
    flag = False
    faces = list(map(int, input().split()))
    dice_list.append(Dice(faces))

    if n == 0:
        continue

    dice_a = dice_list[-1]
    for i in range(len(dice_list) - 1):
        dice_b = dice_list[i]

        if (dice_a.get_nums() == dice_b.get_nums()):
            for i in range(1000):
                dice_b.roll(random.randint(0, 3))
                if dice_a.is_equal(dice_b):
                    flag = True
                    break

        flag_list.append(flag)

if any(flag_list):
    print('No')
else:
    print('Yes')
### BUGGY CODE ###

import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


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

d={}
for s in range(m):
    a,b,c=map(int,input().split())
    if a in d:
        d[a].append([b,c])
    else:
        d[a]=[[b,c]]

#print(d)


def BELLMANFORD(point,d,p):
    cost       = [-float("inf") for node in range(n+1)]  #infじゃないと閉路検出がうまくいかない
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

    
if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


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

d={}
for s in range(m):
    a,b,c=map(int,input().split())
    if a in d:
        d[a].append((b,c))
    else:
        d[a]=[(b,c)]

#print(d)


def BELLMANFORD(point,d,p):
    cost       = [-float("inf") for node in range(n+1)]  #infじゃないと閉路検出がうまくいかない
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

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

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

A = []
A.append(list('@'*(W+2)))
for i in range(H):
  a = list('@'+input()+'@')
  A.append(a)
A.append(list('@'*(W+2)))

vy = [0,0,1,-1]
vx = [1,-1,0,0]
b_count = 0
ans = 0
while(1):
 b_count = 0
 tmp_list = []
 for i in range(H+2):
  if((i != 0)and(i != H+1)):
   for j in range(W+2):
    if((j != 0)and(j != W+1)):
     if(A[i][j] == '#'): 
       tmp_list.append([i,j])
       b_count += 1
 
 #終了条件
 if(b_count == H*W):
  break
  
 for i in range(len(tmp_list)):
  tmp_y,tmp_x = tmp_list[i][0],tmp_list[i][1]
  for j in range(4):
    if(A[tmp_y+vy[j]][tmp_x+vx[j]] == '.'):
      A[tmp_y+vy[j]][tmp_x+vx[j]] = '#'
 
 ans += 1
 
print(ans)      

### FIXED CODE ###

from collections import deque

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

by,bx= deque(),deque()
Map = []
Map.append('@'*(W+2))
for i in range(H):
  c = list('@'+input()+'@')
  Map.append(c)
  for j in range(W+2):
    if(c[j] == '#'):
      Map[i+1][j] = 0
      by.append(i+1),bx.append(j)
Map.append('@'*(W+2))      

cnt = 0
while(1):
  if(len(by) == 0):
    break
   
  ny,nx = by.popleft(),bx.popleft()
  vec_y,vec_x = [1,-1,0,0],[0,0,1,-1]
  for i in range(4):
      if(Map[ny+vec_y[i]][nx+vec_x[i]] == '.'):
         Map[ny+vec_y[i]][nx+vec_x[i]] = Map[ny][nx] + 1
         cnt = max(cnt, Map[ny][nx] + 1) 
         by.append(ny+vec_y[i]),bx.append(nx+vec_x[i])
  
print(cnt)
### BUGGY CODE ###

from collections import defaultdict
n,p = map(int,input().split())
s = list(input())
s = [int(x) for x in s]
d1 = defaultdict(int)

ans = 0
for x in s:
    d2 = defaultdict(int)
    for y in d1.keys():
        z = (10*y + x)%p
        d2[z] += d1[y]
    d2[x%p] += 1
    ans += d2[0]
    d1 = d2

print(ans)

### FIXED CODE ###

from collections import defaultdict
n,p = map(int,input().split())
s = list(input())
s = [int(x) for x in s][::-1]
if p != 2 and p != 5:
    ans = 0
    data = [0 for _ in range(p)]
    now = 1
    data[0] = 1
    a = 0
    for i in range(n):
        a += now*s[i]
        a %=p
        data[a]+=1
        now *= 10
        now %= p
    for x in data:
        ans += x*(x-1)//2
    print(ans)
if p == 2:
    ans = 0
    for i,x in enumerate(s[::-1]):
        if x%2 == 0:
            ans += i+1
    print(ans)
if p == 5:
    ans = 0
    for i,x in enumerate(s[::-1]):
        if x%5 == 0:
            ans += i+1
    print(ans)