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

S = list(input())
size = len(S)
ans = 0

while True:
    i = 0
    fg = False
    while i < size -1:
        if S[i] == 'B' and S[i+1] == 'W':
            ans += 1
            S[i] = 'W'
            S[i+1] = 'B'
            fg = True
            break
        i += 1
    if fg == False:
        break
print(ans)

### FIXED CODE ###

import sys
import math
S = list(input())
size = len(S)

B_size = sum([ 1 for i in S if i == 'B' ])
if B_size == 0:
    print(0)
    sys.exit()

B_size2 = sum([ i for i,j in enumerate(S) if j == 'B' ])
ans= sum(range(0, size)[-B_size:]) - B_size2
print(ans)
### BUGGY CODE ###

L, R = map(int, input().split())
R = min(R, L+4038)
ans = 10e+10
for i in range(L, R):
    for j in range(i+1, R+1):
        ans = min((i*j)%2019, ans)
print(ans)


### FIXED CODE ###

L, R = map(int, input().split())
R = min(R, L+4038)
ans = 2018
for i in range(L, R):
    for j in range(i+1, R+1):
        ans = min((i*j)%2019, ans)
print(ans)
### BUGGY CODE ###

from collections import deque
import sys
sys.setrecursionlimit(10**8)

def dp(dp_l,i,N,xs,cum,now):
    if i == N:
        if now == 0:
            return True
        return False
    if abs(now) > cum[i]:
        return False
    if dp_l[i][abs(now)] != -1:
        return dp_l[i][abs(now)]
    dp_l[i][abs(now)] = dp(dp_l,i+1,N,xs,cum,now+xs[i]) or dp(dp_l,i+1,N,xs,cum,now-xs[i])
    return dp_l[i][abs(now)]

def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    if len(fs)<3:
        if x != fs[0]:
            return False
        if len(fs)==1:
            if y == 0:
                return True
            else:
                return False
        if y != fs[1] and y != -fs[1]:
            return False
        return True
    xs = fs[2::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    dp_l = [[-1]*(cum[0]+1) for _ in range(N)]
    if not dp(dp_l,0,N,xs,cum,fs[0]-x):
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    dp_l = [[-1]*(cum[0]+1) for _ in range(M)]
    if not dp(dp_l,0,M,ys,cum,-y):
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

### FIXED CODE ###

from collections import deque
import sys
sys.setrecursionlimit(10**8)

def dp(dp_l,i,N,xs,cum,now):
    if i == N:
        if now == 0:
            return True
        return False
    if abs(now) > cum[i]:
        return False
    if dp_l[i][abs(now)] != -1:
        return dp_l[i][abs(now)]
    dp_l[i][abs(now)] = dp(dp_l,i+1,N,xs,cum,now+xs[i]) or dp(dp_l,i+1,N,xs,cum,now-xs[i])
    return dp_l[i][abs(now)]

def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    if len(fs)<3:
        if x != fs[0]:
            return False
        if len(fs)==1:
            if y == 0:
                return True
            else:
                return False
        if y != fs[1] and y != -fs[1]:
            return False
        return True
    xs = fs[2::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    dp_l = [[-1]*(cum[0]+1) for _ in range(N)]
    if not dp(dp_l,0,N,xs,cum,fs[0]-x):
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    dp_l = [[-1]*(cum[0]+1) for _ in range(M)]
    if not dp(dp_l,0,M,ys,cum,-y):
        return False
    return True
if check():
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

N,K = map(int,input().split())
stones = list(map(int,input().split()))
dp =[0] *N

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

print(dp[-1])


### FIXED CODE ###

N,K = map(int,input().split())
stones = list(map(int,input().split()))
dp =[0] *N

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

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

import sys
import os

def file_input():
    f = open('../Beginner_Contest_165/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # input()
    A,B,N=map(int, input().split())

    max=0
    # for x in range(N+1):
    #     # ans=int(A*x/B)-A*int(x/B)
    #     min,_=math.modf(x/B)
    #     # ans=int(A*min)
    #     # print(min)
    #     if x==0:
    #         max=min
    #     elif min > max:
    #         max=min
    #     else:
    #         break

    max=0
    max_x=0
    reg=[]

    for x in range(N+1):
        rem = x%B
        if rem in reg:
            break
        else:
            reg.append(rem)
        if rem > max:
            max=rem
            max_x=x

    # min,_=math.modf(max_x/B)

    print(int(A*max_x/B)-A*int(max_x/B))

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import os

def file_input():
    f = open('../Beginner_Contest_165/input.txt', 'r')
    sys.stdin = f

def main():
   # file_input()
    # input()
    A,B,N=map(int, input().split())

    max=0
    # for x in range(N+1):
    #     # ans=int(A*x/B)-A*int(x/B)
    #     min,_=math.modf(x/B)
    #     # ans=int(A*min)
    #     # print(min)
    #     if x==0:
    #         max=min
    #     elif min > max:
    #         max=min
    #     else:
    #         break

    # max=0
    # max_x=0
    # first=0
    #
    # for x in range(N+1):
    #     rem = x%B
    #     if x == 0:
    #         first=rem
    #     elif rem == first:
    #         break
    #     if rem > max:
    #         max=rem
    #         max_x=x

    if N<B:
        max_x=N
    else:
        max_x=B-1

    # min,_=math.modf(max_x/B)

    print(int(A*max_x/B)-A*int(max_x/B))

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

x = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a.sort()
a.reverse()

b.sort()
b.reverse()

c.sort()
c.reverse()

l = []

for i in range(x[0]):
    for j in range(x[1]):
        for k in range(x[2]):
            l.append(a[i]+b[j]+c[k])

l.sort()
l.reverse()

for i in range(x[3]):
    print(l[i])

### FIXED CODE ###

x,y,z,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

d = []
for xx in a:
    for yy in b:
        d.append(xx+yy)
d.sort()
d.reverse()


e = []
if len(d)>3000:
    for i in range(3000):
        for zz in c:
            e.append(d[i]+zz)
else:
    for xy in d:
        for zz in c:
            e.append(xy+zz)
e.sort()
e.reverse()

for i in range(k):
    print(e[i])
### BUGGY CODE ###

S = input()
Q = int(input())
Flag = True

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            inv_add = QueryList[2][::-1]
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

if Flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end="")

### FIXED CODE ###

from collections import deque

d = deque()
S = input()
d.append(S)
Q = int(input())
Flag = True
 
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                d.appendleft(QueryList[2])
            else:
                d.append(QueryList[2])
        else:
            if QueryList[1] =="2":
                d.appendleft(QueryList[2])
            else:
                d.append(QueryList[2])

d_string = "".join(list(d))

if not Flag:
    d_string = d_string[::-1]    
print(d_string)
### BUGGY CODE ###

n,l=map(int,input().split())
s=[]
for i in range(n):
    s.append(input())
s=list(sorted(s))
print(''.join(s))


### FIXED CODE ###

n,l=map(int,input().split())
s=[]
for i in range(n):
    s.append(input())
s=list(sorted(s))
print(''.join(s))
### BUGGY CODE ###

def main():
    import bisect
    import math
    n,d,a=map(int,input().split())
    xh=[list(map(int,input().split())) for i in range(n)]
    xh.sort()
    x=[xh[i][0] for i in range(n)]
    h=[xh[i][1] for i in range(n)]
    cnt=0
    for i in range(n):
        if h[i]>0:
            m=math.ceil(h[i]/a)
            cnt+=m
            k=bisect.bisect_right(x,x[i]+2*d,lo=i)
            for j in range(i,k):
                h[j]-=(m*a)
    print(cnt)
main()


### FIXED CODE ###

import bisect
import math
n,d,a=map(int,input().split())
xh=[list(map(int,input().split())) for i in range(n)]
xh.sort()
x=[xh[i][0] for i in range(n)]
h=[xh[i][1] for i in range(n)]
g=[0]*n
cnt=0
for i in range(n):
    #print(h)
    if h[i]>0:
        m=math.ceil(h[i]/a)
        cnt+=m
        k=bisect.bisect_right(x,x[i]+2*d,lo=i)
        if i!=n-1:
            h[i]-=m*a
            g[i]-=m*a
            if k<n:
                g[k]+=m*a
            g[i+1]+=g[i]
            h[i+1]+=g[i+1]
    else:
        if i!=n-1:
            g[i+1]+=g[i]
            h[i+1]+=g[i+1]
print(cnt)
### BUGGY CODE ###

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby



num = int(stdin.readline().rstrip())
data = stdin.readline().rstrip().split()
res = []
for el in range(1, num + 1):
    res.append(str(data.index(str(el)) + 1))
print(' '.join(res))


### FIXED CODE ###

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''


num = int(stdin.readline().rstrip())
data = stdin.readline().rstrip().split()
lis = [''] * num
res = ''
count = 1
for el in data:
    lis[int(el)-1] = str(count)
    count += 1
print(' '.join(lis))
### BUGGY CODE ###

N = int(input())
S = input()
result = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)

### FIXED CODE ###

N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
r = S.count('R')
g = S.count('G')
b = S.count('B')
target = set('RGB')
result = r * g * b
for i in range(N):
    for j in range(1, min(i, (N - i - 1)) + 1):
        if target == set(S[i] + S[i-j] + S[j + i]):
            result -= 1
print(result)
### BUGGY CODE ###

import sys


fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = fin().rstrip("\n")[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
    base = 10 ** (i - 1)
    if c != '?':
        d = int(c) * base % 13
        for r in range(13):
            dp[i][(d + r) % 13] += dp[i - 1][r]
            dp[i][(d + r) % 13] %= MOD
    else:
        for d in range(10):
            d = d * base % 13
            for r in range(13):
                dp[i][(d + r) % 13] += dp[i - 1][r]
                dp[i][(d + r) % 13] %= MOD
print(dp[-1][5])


### FIXED CODE ###

# import sys


# fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
    base = pow(10, i - 1, 13)
    if c != '?':
        d = int(c) * base % 13
        for r in range(13):
            c = (d + r) % 13
            dp[i][c] += dp[i - 1][r]
            dp[i][c] %= MOD
    else:
        for d in range(10):
            d = d * base % 13
            for r in range(13):
                c = (d + r) % 13
                dp[i][c] += dp[i - 1][r]
                dp[i][c] %= MOD
print(dp[-1][5])
### BUGGY CODE ###

N, M = map( int, input().split() )
a = list( map( int, input().split() ) )
 
d = []
for i in range( M ):
  b, c = map( int, input().split() )
  d += [ c ] * b
d.sort()
a += d
a.sort()
print(sum(a[-N:]))

### FIXED CODE ###

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))
### BUGGY CODE ###


N,M=[int(x) for x in str(input()).split()]
lim=int(M**0.5)+10000000

prime_sieve=[1 for i in range(lim)]


prime_sieve[0]=0
prime_sieve[1]=0

prime=[]
count=[]
for i in range(2,lim):
  if prime_sieve[i]==1:
    prime.append(i)
    count.append(0)
    for j in range(2,(lim-1)//i+1):
      prime_sieve[i*j]=0

for i in range(len(prime)):
  while M%prime[i]==0:
    count[i]+=1
    M=M//prime[i]
  if M==1:
    break


num=10**9+7
x=1
factorial=[1]
for i in range(1,2*10**5+1):
  x=(i*x)%num
  factorial.append(x)
 
rfactorial=[]
for i in factorial:
  rfactorial.append(pow(i,num-2,num))
 

def c(n,k):
  return (((factorial[n]*rfactorial[k])%num)*rfactorial[n-k])%num


ans=1
for i in range(len(prime)):
  ans=(ans*c(N-1+count[i],count[i]))%num
print(ans)


### FIXED CODE ###


N,M=[int(x) for x in str(input()).split()]
lim=int(M**0.5)+100005




num=10**9+7
x=1
factorial=[1]
for i in range(1,2*10**5+1):
  x=(i*x)%num
  factorial.append(x)
 
rfactorial=[]
for i in factorial:
  rfactorial.append(pow(i,num-2,num))
 

def c(n,k):
  return (((factorial[n]*rfactorial[k])%num)*rfactorial[n-k])%num


ans=1
for i in range(2,int(M**0.5)+2):
  count=0
  while M%i==0:
    count+=1
    M=M//i
  ans=(ans*c(N+count-1,count))%num
if M>1:
  ans=(ans*c(N,1))%num

print(ans)
### BUGGY CODE ###

MOD = 998244353
FACT_MAX = 2 * 10 ** 5
fact = [(f := 1 if not i else (f := f * i % MOD)) for i in range(FACT_MAX)]
def comb(n, r):
    return fact[n] * pow(fact[n - r], MOD - 2, MOD) * pow(fact[r], MOD - 2, MOD) % MOD
N, M, K = map(int, input().split())
total = 0
for k in range(K + 1):
    total = (total + comb(N - 1, k) * M * pow(M - 1, N - k - 1, MOD)) % MOD
print(total)

### FIXED CODE ###

MOD = 998244353
FACT_MAX = 2 * 10 ** 5
fact = [1] * FACT_MAX
for i in range(1, FACT_MAX):
    fact[i] = fact[i - 1] * i % MOD
def comb(n, r):
    return fact[n] * pow(fact[n - r], MOD - 2, MOD) * pow(fact[r], MOD - 2, MOD) % MOD
N, M, K = map(int, input().split())
total = 0
for k in range(K + 1):
    total = (total + comb(N - 1, k) * M * pow(M - 1, N - k - 1, MOD)) % MOD
print(total)
### BUGGY CODE ###

import sys
def rec(N,dp,i,j,k):
    if dp[i][j][k]>=0:return dp[i][j][k]
    if all([i==0,j==0,k==0]):return 0.0
    res=0.0
    if i>0:res+=rec(N,dp,i-1,j,k)*i
    if j>0:res+=rec(N,dp,i+1,j-1,k)*j
    if k>0:res+=rec(N,dp,i,j+1,k-1)*k
    res+=N
    res*=1.0/(i+j+k)
    dp[i][j][k]=res
    return dp[i][j][k]
def main():
    N=int(sys.stdin.readline())
    a=tuple(map(int,sys.stdin.readline().split()))
    n1,n2,n3=a.count(1),a.count(2),a.count(3)
    dp=[[[-1.0 for _ in range(N+1)] for _ in range(N+1)] for _ in range(N+1)]
    print(rec(N,dp,n1,n2,n3))
if __name__=='__main__':main()


### FIXED CODE ###

import sys
def rec(N,dp,i,j,k):
    if dp[i][j][k]>=0.0:return dp[i][j][k]
    if all([i==0,j==0,k==0]):return 0.0
    res=0.0
    if i>0:res+=rec(N,dp,i-1,j,k)*i
    if j>0:res+=rec(N,dp,i+1,j-1,k)*j
    if k>0:res+=rec(N,dp,i,j+1,k-1)*k
    res=(res+N)*1.0/(i+j+k)
    dp[i][j][k]=res
    return dp[i][j][k]
def main():
    N=int(sys.stdin.readline())
    a=tuple(map(int,sys.stdin.readline().split()))
    n1,n2,n3=a.count(1),a.count(2),a.count(3)
    dp=[[[-1.0 for _ in range(n3+1)] for _ in range(n2+n3+1)] for _ in range(n1+n2+n3+1)]
    print(rec(N,dp,n1,n2,n3))
if __name__=='__main__':main()
### BUGGY CODE ###

N=int(input())

ans = 0
for i in range(1,N+1):
    j = N//i
    ans += int(i*j*(j+1)/2)

print(ans)

### FIXED CODE ###

N=int(input())

ans = 0
for i in range(1,N+1):
    j = N//i
    ans += i*j*(j+1)//2

print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def find_index(iterable, func, from_i):
    i = from_i
    l = len(iterable)
    while i<l and not func(iterable[i]):
        i += 1
    return i

def insert(deq, i, x):
    l = len(deq)
    if i < l/2:
        deq.rotate(-i)
        deq.appendleft(x)
        deq.rotate(i)
    else:
        k = l-i
        deq.rotate(k)
        deq.append(x)
        deq.rotate(-k)

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1])
    i = 0
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c, i)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        #cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        for _ in range(change_num):
            insert(cards, i, c)
            cards.popleft()
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



### FIXED CODE ###

#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

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

n,k = map(int, input().split())
binary = [0 for _ in range(n)]
ls = list(map(int, input().split()))
for i in ls:
    binary[i-1] += 1
binary = sorted(binary)

ans = 0
while k < len(binary):
    ans += binary[0]
    binary.pop(0)
print(ans)

### FIXED CODE ###

from collections import Counter

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

c = Counter(A)
d = sorted(c.values())
l = len(d)
print(sum(d[0:l-K]))
### BUGGY CODE ###

import math
from math import floor
A,B,N = map(int,input().split())
maxi = 0
Npower = 0
diff = 0
S = N
while S/10 >= 1:
    Npower += 1
    S /=10
if Npower >= 7:
    for i in range(0,10**6+10**5):
        maxi = max(floor((A*i)/B)-A*floor(i/B),maxi)
    for i in range(N-5*10**6,N+1):
        maxi = max(floor((A*i)/B)-A*floor(i/B),maxi)
else:
    for i in range(0,N+1):
        maxi = max(floor((A*i)/B)-A*floor(i/B),maxi)
print(max(maxi,0))

### FIXED CODE ###

import math
from math import floor
A,B,N = map(int,input().split())
def f(x):
  return floor(A*x/B) - A*floor(x/B)
print(f(min(B-1,N)))
### BUGGY CODE ###

n,m = list(map(int,input().split()))
h = list(map(int,input().split()))
d = [[0] * n for _ in range(n)]

for i in range(m):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  d[a][b] = 1
  d[b][a] = 1 

mh = [0] * n
ans = set()

for i in range(n):
  t = h[i]
  mh[i] = 1
  for j in range(n):
    if d[i][j] == 1 and i != j:
      if t <= h[j]:
        mh[i] = 0
        break
  if mh[i] == 1: ans.add(i)

print(len(ans))

### FIXED CODE ###


n,m = list(map(int,input().split()))
h = list(map(int,input().split()))
d = [1] * n
for i in range(m):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  if h[a] > h[b]:
    d[b] = 0
  elif h[b] > h[a]:
    d[a] = 0
  else:
    d[a] = 0
    d[b] = 0

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

H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())

left = [[-1 for j in range(W)] for i in range(H)]
right = [[W for j in range(W)] for i in range(H)]
up = [[-1 for j in range(W)] for i in range(H)]
down = [[H for j in range(W)] for i in range(H)]

# from left upper to right down
up_wall = [-1 for i in range(W)]
for h in range(H):
    left_wall = -1    
    for w in range(W):
        if S[h][w] == '#':
            left_wall = w
            up_wall[w] = h
        left[h][w] = left_wall
        up[h][w] = up_wall[w]

# from right down to left up
down_wall = [H for i in range(W)]
for h in range(H - 1, -1, -1):
    right_wall = W
    for w in range(W - 1, -1, -1):
        if S[h][w] == '#':
            right_wall = w
            down_wall[w] = h
        right[h][w] = right_wall
        down[h][w] = down_wall[w]

# print(left)
# print(right)
# print(up)
# print(down)

max_score = 0
for h in range(H):
    for w in range(W):
        left_score = (w - left[h][w])
        right_score = (right[h][w] - w)
        up_score = (h - up[h][w])
        down_score = (down[h][w] - h)
        score = left_score + right_score + up_score + down_score - 3
        # print(h, w, score, left_score, right_score, up_score, down_score)
        if max_score < score:
            max_score = score
print(max_score)

### FIXED CODE ###

H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())

left = [[-1 for j in range(W)] for i in range(H)]
right = [[W for j in range(W)] for i in range(H)]
up = [[-1 for j in range(W)] for i in range(H)]
down = [[H for j in range(W)] for i in range(H)]

# from left upper to right down
up_wall = [-1 for i in range(W)]
for h in range(H):
    left_wall = -1    
    for w in range(W):
        if S[h][w] == '#':
            left_wall = w
            up_wall[w] = h
        left[h][w] = left_wall
        up[h][w] = up_wall[w]

# from right down to left up
down_wall = [H for i in range(W)]
for h in range(H - 1, -1, -1):
    right_wall = W
    for w in range(W - 1, -1, -1):
        if S[h][w] == '#':
            right_wall = w
            down_wall[w] = h
        right[h][w] = right_wall
        down[h][w] = down_wall[w]

# print(left)
# print(right)
# print(up)
# print(down)

max_score = 0
for h in range(H):
    for w in range(W):
        left_score = (w - left[h][w])
        right_score = (right[h][w] - w)
        up_score = (h - up[h][w])
        down_score = (down[h][w] - h)
        score = left_score + right_score + up_score + down_score - 3
        # print(h, w, score, left_score, right_score, up_score, down_score)
        if max_score < score:
            max_score = score
print(max_score)
### BUGGY CODE ###

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

import itertools
a = []
for i in range(q):
    a.append(input().split())

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n):
    ans_ = 0
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_

print(ans)

### FIXED CODE ###

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

import itertools
a = []
for i in range(q):
    a.append(input().split())

suma = sum([int(aa[3]) for aa in a])

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n-1):
    ans_ = 0
    v = [0]+list(v)
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_
        if ans == suma:
            break
print(ans)