N = int(eval(input()))

s = [eval(input()) for _ in [0]*N]

ni = [0]*N

for i,si in enumerate(s):

    for j,c in enumerate(si[:-1]):

        if c == "A" and si[j+1] == "B":

            ni[i] += 1

cntA,cntB,cntAB = 0,0,0

for si in s:

    if si[0]=="B" and si[-1]=="A":

        cntAB += 1

    elif si[0]=="B":

        cntB += 1

    elif si[-1]=="A":

        cntA += 1

ans = sum(ni) + max(0,cntAB - (cntA==0 and cntB==0)) + min(cntA,cntB)

print(ans)
N = int(eval(input()))

S = [eval(input()) for _ in [0]*N]



ans = 0

BA = 0

BX = 0

XA = 0

for s in S:

    ans += s.count("AB")

    if s[0]=="B" and s[-1]=="A":BA+=1

    elif s[0]=="B":BX+=1

    elif s[-1]=="A":XA+=1

ans += min(BX,XA) + max(0,BA - (BX==XA==0))

print(ans)
N = int(eval(input()))
s = [eval(input()) for _ in [0] * N]
ni = [0] * N
for i, si in enumerate(s):
    for j, c in enumerate(si[:-1]):
        if c == "A" and si[j + 1] == "B":
            ni[i] += 1
cntA, cntB, cntAB = 0, 0, 0
for si in s:
    if si[0] == "B" and si[-1] == "A":
        cntAB += 1
    elif si[0] == "B":
        cntB += 1
    elif si[-1] == "A":
        cntA += 1
ans = sum(ni) + max(0, cntAB - (cntA == 0 and cntB == 0)) + min(cntA, cntB)
print(ans)

N = int(eval(input()))
S = [eval(input()) for _ in [0] * N]
ans = 0
BA = 0
BX = 0
XA = 0
for s in S:
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B":
        BX += 1
    elif s[-1] == "A":
        XA += 1
ans += min(BX, XA) + max(0, BA - (BX == XA == 0))
print(ans)

-s = [eval(input()) for _ in [0] * N] -ni = [0] * N -for i, si in enumerate(s): -    for j, c in enumerate(si[:-1]): -        if c == "A" and si[j + 1] == "B": -            ni[i] += 1 -cntA, cntB, cntAB = 0, 0, 0 -for si in s: -    if si[0] == "B" and si[-1] == "A": -        cntAB += 1 -    elif si[0] == "B": -        cntB += 1 -    elif si[-1] == "A": -        cntA += 1 -ans = sum(ni) + max(0, cntAB - (cntA == 0 and cntB == 0)) + min(cntA, cntB) +S = [eval(input()) for _ in [0] * N] +ans = 0 +BA = 0 +BX = 0 +XA = 0 +for s in S: +    ans += s.count("AB") +    if s[0] == "B" and s[-1] == "A": +        BA += 1 +    elif s[0] == "B": +        BX += 1 +    elif s[-1] == "A": +        XA += 1 +ans += min(BX, XA) + max(0, BA - (BX == XA == 0))