Problem:
 Score : 400 points    Problem Statement Snuke has N strings. The i -th string is s_i .  Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.      Constraints  1 \leq N \leq 10^{4}  2 \leq |s_i| \leq 10  s_i consists of uppercase English letters.         Input Input is given from Standard Input in the following format:  N  s_1  \vdots  s_N       Output Print the answer.        Sample Input 1 3
ABCA
XBAZ
BAD      Sample Output 1 2  For example, if we concatenate ABCA , BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB .       Sample Input 2 9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA      Sample Output 2 4       Sample Input 3 7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA      Sample Output 3 4

Solution (Python):
import sys
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    output = 0
    x = y = z = 0
    for _ in range(N):
        s = sys.stdin.readline().rstrip()
        output += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A': y += 1
        elif s[-1] == 'A': x += 1
        elif s[0] == 'B': z += 1
        
    if y == 0:
        output += min(x, z)
        print(output)
        return

    if x > 0:
        output += y + min(1, z) + max(min(x-1, z-1), 0)
        print(output)
        return

    output += y - 1 + min(1, z)
    print(output)

    return

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
a = 0
b = 0
c = 0
cnt = 0
for i in s:
    cnt += i.count("AB")
    if i[0] == "B" and i[-1] == "A":
        a += 1
    elif i[0] == "B":
        b += 1
    elif i[-1] == "A":
        c += 1
if a == 0:
    print(min(b, c) + cnt)
else:
    if b + c == 0:
        print(cnt + a - 1)
    else:
        print(cnt + a + min(b, c))

Solution (Python):
n = int(input())
res = 0
numa = numb = numboth = 0
for _ in range(n):
    s = input()
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        numboth += 1
    elif s[0] == "B":
        numb += 1
    elif s[-1] == "A":
        numa += 1




if numboth == 0:
    res += min(numa, numb)
else:
    if numa == 0 and numb == 0:
        res += numboth - 1
    else:
        res += numboth
        res += min(numa, numb)
print(res)

Solution (Python):
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[0] == "B":
        if s[-1] == "A":
            ba += 1
        else:
            b += 1
    elif s[-1] == "A":
        a += 1
if a >= 1 or b >= 1:
    print(ans+ba+min(a,b))
else:
    print(ans+max(0,ba-1))

Solution (Python):
n = int(input())
slist = []
ans = 0
startB, endA = 0, 0
b_a = 0
for _ in range(n):
    s = list(input())
    for j in range(len(s) - 1):
        if ("".join(s[j:j + 2]) == "AB"):
            ans += 1
    startB += 1 if s[0] == "B" else 0
    endA += 1 if s[len(s) - 1] == "A" else 0
    b_a += 1 if s[0] == "B" and s[len(s) - 1] == "A" else 0

if startB == b_a and endA == b_a:
    startB -= 1

print(ans+min(max(0, startB), max(0, endA)))

Solution (Python):
N=int(input())
b,ba,a=0,0,0
ans=0
for i in range(N):
    s=input()
    for i in range(len(s)-1):
        if s[i:i+2]=='AB':
            ans+=1
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[-1]=='A':
        a+=1
    elif s[0]=='B':
        b+=1
if b==0 and a==0:
    print(ans+max(ba-1,0))
else:
    print(ans+ba+min(a,b))

Solution (Python):

t = open(0).read()
c1 = c2 = c3 = 0
for s in t.split():
    if s[-1] == "A" and s[0] == "B":
        c1 += 1
    elif s[-1] == "A":
        c2 += 1
    elif s[0] == "B":
        c3 += 1
if c1:
    a = c1 + min(c2, c3) if c2 + c3 > 0 else c1 - 1
else:
    a = min(c2, c3)

print(t.count("AB") + a)

Solution (Python):
n = int(input())
s = []
for i in range(n):
    s.append(input())

cnt_end_a = 0
cnt_bgn_b = 0
cnt_end_a_bgn_b = 0

ans = 0
for i in range(n):
    if s[i][0] == 'B':
        if s[i][len(s[i])-1] == 'A':
            cnt_end_a_bgn_b += 1
        else:
            cnt_bgn_b += 1
    elif s[i][len(s[i])-1] == 'A':
        cnt_end_a += 1
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            ans += 1



if cnt_end_a_bgn_b == 0:
    ans += min(cnt_bgn_b, cnt_end_a)
elif cnt_end_a == 0 and cnt_bgn_b == 0:
    ans += cnt_end_a_bgn_b - 1
elif cnt_end_a == 0:
    ans += cnt_end_a_bgn_b
elif cnt_bgn_b == 0:
    ans += cnt_end_a_bgn_b
else:
    ans += cnt_end_a_bgn_b + min(max(cnt_bgn_b, cnt_end_a), min(cnt_bgn_b, cnt_end_a))
print(ans)

Solution (Python):
n = int(input())

ans = 0
last_a = 0
first_b = 0
cnt = 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B":
        first_b += 1
    if s[-1] == "A" and s[0] == "B":
        cnt += 1

if last_a == first_b & cnt > 0:
    ans += min(last_a, first_b) - 1
else:
    ans += min(last_a, first_b)

print(ans)

Solution (Python):
def solve():
    N = int(input())
    s = []
    ans = 0

    A_cnt = 0
    B_cnt = 0
    BA_cnt = 0
    for i in range(N):
        s_tmp = input()
        s.append(s)
        for j in range(len(s_tmp)-1):
            if s_tmp[j] == "A" and s_tmp[j+1] == "B":
                ans += 1
        
        if s_tmp[0] == "B" and s_tmp[-1] == "A":
            BA_cnt += 1
        elif s_tmp[0] == "B":
            B_cnt += 1
        elif s_tmp[-1] == "A":
            A_cnt += 1
    
    if BA_cnt > 0:
        ans += BA_cnt - 1
        if A_cnt > 0:
            A_cnt -= 1
            ans += 1
        if B_cnt > 0:
            B_cnt -= 1
            ans += 1
    
    ans += min(A_cnt,B_cnt)
    
    print(ans)
if __name__ == '__main__':
    solve()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
from fractions import gcd
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

from itertools import permutations
from math import factorial, hypot





N = getN()
cnt = 0
last_a = 0
first_b = 0
double = 0
for i in range(N):
    s = input()
    for j in range(len(s) - 1):
        if s[j] == 'A' and s[j + 1] == 'B':
            cnt += 1
    if s[0] == 'B' and s[-1] == 'A':
        double += 1
    elif s[0] == 'B':
        first_b += 1
    elif s[-1] == 'A':
        last_a += 1
opt = 0
if first_b == 0 and last_a == 0 and double != 0:
    opt = double - 1
else:
    opt = min(first_b + double, last_a + double)
print(cnt + opt)

Solution (Python):
N = int(input())
ba, bc, ca = 0, 0, 0
c = 0
for _ in range(N):
    ss = input()
    if ss[0] == 'B':
        if ss[-1] =='A':
            ba += 1
        else: bc += 1
    else:
        if ss[-1] == 'A':
            ca += 1
    c += ss.count('AB')

cnt = max(ba - 1, 0)
if ba >= 1:
    if ca >= 1:
        ca -= 1
        cnt += 1
    if bc >= 1:
        bc -= 1
        cnt += 1
cnt += min(ca, bc)

print(cnt + c)

Solution (Python):
N = int(input())

ab = 0
x = 0
y = 0
z = 0

for _ in range(N):
    s = input()
    ab += s.count("AB")
    if (s[-1] == "A" and s[0] == "B"):
        z += 1
    elif (s[-1] == "A"):
        x += 1
    elif (s[0] == "B"):
        y += 1

if (not x and not y) and z:
    print(ab + z-1)
else:
    print(ab + min(x+z, y+z))

Solution (Python):
N=int(input())
S=[]
cnt=Alist=Blist=0
key=False
for i in range(N):
    s=input()
    cnt+=s.count("AB")
    S=list(s)
    if S[0] == "B":
        Blist+=1
    if S[-1] == "A":
        Alist+=1
    if (S[0] == "B" and S[-1] != "A") or (S[0] != "B" and S[-1] == "A"):
        key = True

cnt += min(Alist,Blist)
if key or min(Alist,Blist)==0:
    print(cnt)
else:
    print(cnt-1)

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque 


from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush




def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N=len(v)
    size=len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def main():
    mod = 10**9+7
    

    N = int(input())
    
    
    
    
    count=0

    ab=0
    a=0
    b=0
    for i in range(N):
        s=input()
        ls=len(s)
        for j in range(ls-1):
            if s[j:j+2]=='AB':
                count+=1
        if s[0]=='B' and s[-1]=='A':
            ab+=1
        elif s[-1]=='A':
            a+=1
        elif s[0]=='B':
            b+=1
    if a==0 and b==0:
        print(count+max(ab-1,0))
        exit()
    ans=min(a,b)+count
    ret=abs(a-b)
    if ret<=ab:
        ans+=ab
    else:
        ans+=ab
    print(ans)
    

    


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1
    for s1, s2 in zip(S, S[1:]):
        if (s1 == 'A') and (s2 == 'B'):
            cntab += 1


if N == cnse:
    print(cntab + N - 1)
elif cnse == 0:
    print(cntab + min(cns, cne))
else:
    if (cns == 0) and (cne == 0):
        print(cntab + cnse - 1)
    elif (cns == 0) or (cne == 0):
        print(cntab + cnse)
    else:
        print(cntab + cnse + 1 + min(cns-1, cne-1))

Solution (Python):
n = int(input())
a = 0
b = 0
c = 0
ans = 0
for _ in range(n):
    s = input()
    if s[0]=='B' and s[-1]=='A':
        c+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
if c>0:
    ans += c-1
    if b>0:a += 1
    if a>0:b += 1

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

Solution (Python):
n=int(input())

headB=0
tailA=0
middleAB=0
ck=0
for _ in range(n):
    s=list(input())

    for i in range(1,len(s)):
        if s[i-1]=="A" and s[i]=="B":
            middleAB+=1
    if s[0]=="B":
        headB+=1
    if s[-1]=="A":
        tailA+=1
    if s[0]=="B" and s[-1]=="A":
        ck+=1








if ck>0 and ck==tailA and ck==headB:
    print(middleAB+min(headB,tailA)-1)
else:
    print(middleAB+min(headB,tailA))

Solution (Python):
n = int(input())

a_end = 0
b_start = 0
a_b = 0
cnt = 0

for _ in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
        continue
    if s[0] == "B":
        b_start += 1
    if s[-1] == "A":
        a_end += 1

if a_end == 0 and b_start == 0:
    tmp = max(a_b - 1, 0)
else:
    tmp = min(a_end,  b_start) + a_b
print(cnt + tmp)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = [input().strip() for _ in range(N)]
    a = 0
    b = 0
    ba = 0
    za = 0

    ans = 0
    for s in S:
        if ba == 0 and s[0] == 'B' and s[-1] == 'A':
            ba = 1
        if za == 0 and s[0] != 'B' and s[-1] == 'A':
            za = 1

        ans += s.count("AB")

        if s[-1] == 'A':
            a += 1
        if s[0] == 'B':
            b += 1

    if ba == 1 and za == 0:
        print(ans + min(max(a, b) - 1, min(a, b)))
    else:
        print(ans + min(a, b))


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
l=[input() for i in range(N)]
a_c=0
b_c=0
ab_c=0
ans=0
for i in l:
   ans+=i.count("AB")
   if i[0] == "B" and i[-1] == "A":
      ab_c+=1
      continue
   if i[-1] == "A":
      a_c+=1
   if i[0] == "B":
      b_c+=1
ans+=max(ab_c-1,0)
if a_c > 0 and ab_c>0:
   a_c-=1
   ans+=1
if b_c > 0 and ab_c>0:
   b_c-=1
   ans+=1
ans+=min(a_c,b_c)
print(ans)

Solution (Python):
def cal_single(S,N):
    output = 0
    for i in range(N):
        output += S[i].count("AB")
    return output

def cal_cont(S,N):
    a,b,ab = 0,0,0
    for i in range(N):
        if S[i][0] == "B" and S[i][-1] == "A":
            ab += 1
        elif S[i][0] == "B":
            b += 1
        elif S[i][-1] == "A":
            a += 1

    if ab == 0:
        return min(a,b)
    elif a + b == 0:
        return ab-1
    else:
        return ab + min(a,b)

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    ans = cal_single(S,N)+cal_cont(S,N)

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())

type1 = type2 = type3 = 0

ans = 0
while n > 0:
    s = input()
    ans += s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        type3 += 1
    elif s[-1] == "A":
        type1 += 1
    elif s[0] == "B":
        type2 += 1

    n -= 1

ans += min(type1, type2)

if type3 > 0:
    if type1 == type2 == 0:
        ans += type3 - 1
    else:
        ans += type3

print(ans)

Solution (Python):
import sys


import math
import collections

import bisect

import fractions

import copy
import heapq
import decimal

import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n = ni()
    b_start = 0
    a_end = 0
    b_and_a = 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count("AB")
        flgB = s[0] == "B"
        flgA = s[-1] == "A"
        if flgB and flgA:
            b_and_a += 1
        elif flgB:
            b_start += 1
        elif flgA:
            a_end += 1

    remain_a = max(0, a_end - b_and_a)
    remain_b = max(0, b_start - b_and_a)
    remain_ab_a = max(0, b_and_a - b_start)
    remain_ab_b = max(0, b_and_a - a_end)

    ans += min(b_start, b_and_a)
    ans += min(a_end, b_and_a)
    ans += min(remain_a, remain_b)

    if remain_ab_a == remain_ab_b == b_and_a:
        if min(remain_ab_a, remain_ab_b) > 0:
            ans += min(remain_ab_a, remain_ab_b) - 1
    else:
        ans += min(remain_ab_a, remain_ab_b)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, S


def solve(N, S):
    count = 0
    for s in S:
        for j in range(len(s)):
            if s[j:j+2] == "AB":
                count += 1
    head_b = []
    tail_a = []
    for i, s in enumerate(S):
        if s[0] == "B":
            head_b.append(i)
        if s[-1] == "A":
            tail_a.append(i)
    head_b = list(sorted(head_b))
    tail_a = list(sorted(tail_a))
    if len(head_b) == 0 or len(tail_a) == 0:
        return count
    
    if len(head_b) == len(tail_a):
        is_same = True
        for b, a in zip(head_b, tail_a):
            if b != a:
                is_same = False
                break
        if is_same:
            count -= 1
    return count + min(len(head_b), len(tail_a))


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Solution (Python):

import sys
from collections import deque
from math import sqrt







def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    c = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A' and s[0] != 'B'
        b += s[0] == 'B' and s[-1] != 'A'
        c += s[0] == 'B' and s[-1] == 'A'
        ans += s.replace('AB', "*").count('*')
    if (a+b) > 0:
        print(ans + c + min(a, b))
    else:
        if c:
            c -= 1
        print(ans + c)
    

t = 1

for case in range(1,t+1):
    ans = solve()




"""




"""

Solution (Python):
n,*s=open(0).read().split()
a=0;b=0;ba=0;ans=0
for s_i in s:
  b+=(s_i[0]=='B')*(not s_i[-1]=='A')
  a+=(not s_i[0]=='B')*(s_i[-1]=='A')
  ba+=(s_i[0]=='B')*(s_i[-1]=='A')
  ans+=s_i.count('AB')
print(ans+ba+min(a,b)-(ba>0)*(a+b==0))

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
 
ab = 0
a = 0
b = 0
 
d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1
 
if ab == 0:
	d += min(a,b)
elif a+b == 0:
	d += ab-1
else:
	d += ab+min(a,b)
print (d)

Solution (Python):
n = int(input())

ans = 0
cntA = 0
cntB = 0
cntBA = 0
for i in range(n):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        cntBA += 1
    elif s[-1] == 'A':
        cntA += 1
    elif s[0] == 'B':
        cntB += 1
if cntBA == 0:
    ans += min(cntA, cntB)
elif cntA + cntB == 0:
    ans += cntBA - 1
else:
    ans += cntBA + min(cntA, cntB)

print(ans)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

if ab == 0:
	d += min(a,b)
elif a+b == 0:
	d += ab-1
else:
	d += ab+min(a,b)
print (d)

Solution (Python):
N = int(input())
s = [input() for i in range(N)]

cnt = 0
ab_cnt = 0
b_cnt = 0
a_cnt = 0
for word in s:
    cnt += word.count('AB')
    l = word[0]
    r = word[-1]
    if l == 'B' and r == 'A':
        ab_cnt += 1
    elif l == 'B':
        b_cnt += 1
    elif r == 'A':
        a_cnt += 1

if ab_cnt == 0:
    ans = min(b_cnt, a_cnt)
elif a_cnt + b_cnt > 0:
    ans = ab_cnt + min(b_cnt, a_cnt)
else:
    ans = ab_cnt - 1

print(cnt + ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    BX = 0
    XA = 0
    BA = 0
    AB = 0
    for _ in range(N):
        S = readline().decode('utf-8').strip()
        if S[0]=='B' and S[-1]=='A':
            BA += 1
        elif S[0]=='B':
            BX += 1
        elif S[-1] == 'A':
            XA += 1
        AB += S.count('AB')
    if BA==0:
        ans = AB + min(BX,XA)
    else:
        if XA+BX>0:
            ans = AB + BA + min(BX, XA)
        else:
            ans = AB + BA-1
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N = I()
s = []
for i in range(N):
    s.append(list(input()))

count = 0
enda = 0
startb = 0
both = 0
for i in range(N):
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            count += 1
    if s[i][-1] == 'A':
        enda += 1
    if s[i][0] == 'B':
        startb += 1
    if s[i][-1] == 'A' and s[i][0] == 'B':
        both += 1

temp = count+min(enda,startb)
if enda == startb == both and enda >= 1:
    temp -= 1
print(temp)

Solution (Python):
import sys

readline = sys.stdin.readline


def main():
    N = int(readline())
    cnt = 0; leftB = 0; rightA = 0; same = 0
    for _ in range(N):
        s = readline()[:-1]
        for i in range(len(s)-1):
            subs = s[i:i+2]
            cnt += subs == 'AB'
        leftB += s[0] == 'B'
        rightA += s[-1] == 'A'
        same += s[0] == 'B' and s[-1] == 'A'
    
    if same == leftB == rightA:
        ans = cnt + max(min(N-1, same-1), 0)
    else:
        ans = cnt + min(N-1, leftB, rightA)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=[input() for i in range(n)]
lastA=0
firstB=0
lastAandfirstB=0
ABs=0
for si in s:
    ABs+=si.count("AB")
    firstB+=(si[0]=="B")
    lastA+=(si[-1]=="A")
    lastAandfirstB+=(si[0]=="B")*(si[-1]=="A")

can=min(firstB,lastA)




if can==n or lastAandfirstB==firstB==lastA!=0:
    can=can-1
print(can+ABs)

Solution (Python):
n = int(input())
ans = 0
sb = []
fa = []

def ABCount(s):
    cnt = 0
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            cnt += 1
    return cnt

for i in range(n):
    s = input()
    ans += ABCount(s)
    if s[0] == "B":
        sb.append(i)
    if s[-1] == "A":
        fa.append(i)

if len(sb) == len(fa) and len(sb) > 0:
    sb.sort()
    fa.sort()
    f = False
    for i in range(len(sb)):
        if not sb[i] == fa[i]:
            f = True
            break
    if f:
        ans += len(sb)
    else:
        ans += len(sb)-1
else:
    ans += min(len(sb),len(fa))

print(ans)

Solution (Python):
n=int(input())
s=[None]*n
nA=0
nB=0
nAB=0
ans=0
for i in range(n):
    ss = input()
    ans+= ss.count("AB")
    if ss[0]=="B" and ss[-1]=="A":
        nAB+=1
    elif ss[0]=="B":
        nB+=1
    elif ss[-1]=="A":
        nA+=1
    s[i]=ss

if nAB==0:
    ans+=min(nA,nB)
elif nA==0 and nB==0:
    ans+=nAB-1
elif nA==0:
    ans+=nAB
elif nB==0:
    ans+=nAB
else:
    ans+=nAB+1 + min(nA,nB)-1
print(ans)

Solution (Python):
n = int(input())
S = [input() for _ in range(n)]
XA = 0
BA = 0
BX = 0
cnt = 0
for s in S:
    cnt+=s.count('AB')
    if s[0]!='B' and s[-1]=='A':
        XA+=1
    elif s[0]=='B' and s[-1]=='A':
        BA+=1
    elif s[0]=='B' and s[-1]!='A':
        BX+=1
if BA==0:
    cnt+=min(XA,BX)
else:
    if XA+BX>0:
        cnt+=min(XA,BX)+BA
    else:
        cnt+=BA-1
print(cnt)

Solution (Python):
n = int(input())
r = 0
a = 0
b = 0
ab = 0
for _i in range(n):
    s = input()
    r += s.count("AB")
    B = 0
    A = 0
    if s[-1]=="A":
        a += 1
    if s[0]=="B":
        b += 1
    if s[-1]=="A" and s[0]=="B":
        ab += 1
if ab == a and ab == b:
    print(r+max(0, ab-1))
else:
    print(r+min(a, b))

Solution (Python):
N = int(input())

ends_with_A = 0
starts_with_B = 0
both = 0

count = 0

for _ in range(N):
  s = input()
  count += s.count('AB')
  if s.endswith('A') and s.startswith('B'):
    both += 1
  elif s.endswith('A'):
    ends_with_A += 1
  elif s.startswith('B'):
    starts_with_B += 1


if ends_with_A + starts_with_B > 0:
  print(count + both + min(ends_with_A, starts_with_B))
else:  
  print(count + max(both - 1, 0) + min(ends_with_A, starts_with_B))

Solution (Python):


import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    N=int(input())
    x=0
    y=0
    z=0
    cnt=0
    for i in range(N):
        s=str(input())
        if s[0]=='B' and s[-1]=='A':
            x+=1
        if s[0]=='B' and s[-1]!='A':
            y+=1
        if s[0]!='B' and s[-1]=='A':
            z+=1

        
        for j in range(len(s)-1):
            if s[j:j+2]=='AB':
                cnt+=1
    if x==0:
        print(cnt+min(y,z))
    elif y>0 or z>0: print(cnt+(x-1)+min(y,z)+1)
    else: print(cnt+(x-1)+min(y,z))    
        

    
resolve()

Solution (Python):
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = 0
b = 0
ans = 0
same = 0
for i in s:
    flg = False
    if i[0]=='B':
        flg = True
        b += 1
    if i[-1]=='A':
        a += 1
        if flg:
            same += 1
    for j in range(len(i)-1):
        if i[j:j+2]=='AB':
            ans += 1
ans += min(a, b)
if a == b == same > 0:
    ans -= 1
print(ans)

Solution (Python):
from collections import Counter

DEBUG = False

N = int(input())
S = []
patterns = []
ans = 0
for _ in range(N):
    s = input()
    
    ans += s.count("AB")
    
    
    
    
    
    pattern = ""
    pattern += "2" if (s[0] != "A"  and s[0] != "B") else "1" if s[0] == "B" else "0"
    pattern += "2" if (s[-1] != "A" and s[-1] != "B") else "1" if s[-1] == "B" else "0"
    patterns.append(int(pattern, 3))
    if DEBUG:
        print("s: {}, pattern: {}".format(s, pattern))

if DEBUG:
    print("patterns: {}".format(patterns))
    print("ans: {}".format(ans))

counter = Counter(patterns)
if DEBUG:
    print("counter: {}".format(counter))

ans += counter[3] - 1 if counter[3] else 0
if DEBUG:
    print("ans: {}".format(ans))


num_tail_A = counter[0] + counter[6]
num_head_B = counter[4] + counter[5]
if DEBUG:
    print("num_tail_A: {}, num_head_B: {}".format(num_tail_A, num_head_B))

if not num_tail_A and not num_head_B:
    pass
else:
    num_tail_A += 1 if counter[3] else 0
    num_head_B += 1 if counter[3] else 0
    ans += min(num_tail_A, num_head_B)

print(ans)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]

fi = 0
sec = 0
thi = 0
forth = 0

for i in range(n):
    cnt =  s[i].count("AB")
    if cnt>=1:
        fi+=cnt

    syo = s[i][0]
    saigo = s[i][-1]

    if syo =="B" and saigo == "A":
        sec+=1
    elif syo == "B":
        thi+=1
    elif saigo=="A":
        forth+=1

su=0
if sec>=1:
    su += sec-1

su+=fi

if sec>=1:
    su+=min(1,thi)
    su+=min(1,forth)
    su+=min(thi-min(1,thi),forth-min(1,forth))
else:
    su+=min(thi,forth)


print(su)

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]

cnt = 0
end_a = 0
sta_b = 0
a_b = 0
for s in S:
    cnt += s.count('AB')
    if s[-1]=='A' and s[0]=='B':
        a_b += 1
    elif s[-1]=='A':
        end_a += 1
    elif s[0]=='B':
        sta_b += 1
if a_b>=1:
    cnt += a_b-1
    if end_a>=1:
        end_a -= 1
        cnt += 1
    if sta_b>=1:
        sta_b -=1
        cnt+=1
cnt += min(end_a, sta_b)

print(cnt)

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]

cnt = sum(map(lambda x: x.count('AB'), S))
B_A = len(list(filter(lambda x: x[0] == 'B' and x[-1] == 'A', S)))
B__ = len(list(filter(lambda x: x[0] == 'B' and x[-1] != 'A', S)))
__A = len(list(filter(lambda x: x[0] != 'B' and x[-1] == 'A', S)))

if B_A == 0:
    print(cnt + min(B__, __A))
else:
    if B__ + __A > 0:
        print(cnt + B_A + min(B__, __A))
    else:
        print(cnt + B_A - 1)

Solution (Python):
n=int(input())
xa=0
bx=0
ba=0
ans=0
for i in range(n):
    l=list(input())
    for j in range(len(l)-1):
        if l[j]=='A' and l[j+1]=='B':
            ans += 1
    if l[0]=='B':
        if l[len(l)-1]=='A':
            ba += 1
        else:
            bx += 1
    elif l[len(l)-1]=='A':
        xa += 1
if ba>0:
    ans += ba-1
    if xa>0:
        ans += 1
        xa -= 1
    if bx>0:
        ans += 1
        bx -= 1
ans += min(xa,bx)
print(ans)

Solution (Python):
n = int(input())
S = [str(input()) for _ in range(n)]

a = 0
b = 0
ab = 0
ans = 0
for s in S:
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        ab += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1
    else:
        pass
if ab:
    ans += ab-1
if ab:
    if a:
        ans += 1
        a -= 1
    if b:
        ans += 1
        b -= 1
ans += min(a, b)
print(ans)

Solution (Python):
n = int(input())
S = [str(input()) for _ in range(n)]

a = 0
b = 0
ab = 0
ans = 0
for s in S:
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        ab += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1
    else:
        pass
if ab:
    ans += ab-1
if ab:
    if a:
        ans += 1
        a -= 1
    if b:
        ans += 1
        b -= 1
ans += min(a, b)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  
    if sted > 0:
        ans += (sted - 1)  
        if st > 0:
            ans += 1  
            st -= 1
        if ed > 0:
            ans += 1  
            ed -= 1
    ans += min(st, ed)  
    print(ans)

diverta19_c()

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import defaultdict

n = ni()
s = nsn(n)

cnt = 0
dcnt = 0
dic = defaultdict(int)
for i in range(n):
    for j in range(len(s[i]) - 1):
        if s[i][j] == 'A' and s[i][j + 1] == 'B':
            cnt += 1
    dic['A'] += (s[i][-1] == 'A')
    dic['B'] += (s[i][0] == 'B')
    dcnt += s[i][-1] == 'A' and s[i][0] == 'B'

ans = min(dic['A'], dic['B'])
if dcnt >= 1 and max(dic['A'], dic['B']) == dcnt:
    print(ans - 1 + cnt)
else:
    print(ans + cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n=ii()
backa=0
frontb=0
backafrontb=0
ans=0
for i in range(n):
    s=list(ss())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        backafrontb+=1
    elif s[0]=="B":
        frontb+=1
    elif s[-1]=="A":
        backa+=1


if backafrontb>0:
    ans+=backafrontb-1

    if backa>0:
        ans+=1 
        backa-=1
    if frontb>0:
        ans+=1
        frontb-=1
    ans+=min(backa,frontb)
else:
    ans+=min(backa,frontb)


print(ans)