N = int(eval(input()))

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



a = 0

b = 0

a_b = 0



for s in S:

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

        a_b += 1

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

        a += 1

    elif s[0] == "B":

        b += 1



ans = 0

if a_b > 0:

    ans += a_b - 1

    if a > 0:

        ans += 1

        a -= 1

    if b > 0:

        ans += 1

        b -= 1



ans += min(a,b)



for line in S:

    for i in range(len(line)-1):

        if (line[i],line[i+1]) == ("A","B"):

            ans += 1

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

cnt = 0

A = 0

B = 0

BA = 0

for _ in range(N):

    s = eval(input())

    cnt += s.count("AB")

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

        BA += 1

    elif s[0] == "B":

        B += 1

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

        A += 1



if BA == 0:

    ans = cnt + min(A, B)

else:

    ans = cnt + BA - 1

    if A > 0:

        ans += 1

        A -= 1

    if B > 0:

        ans += 1

        B -= 1

    ans += min(A, B)



print(ans)
N = int(eval(input()))
S = [eval(input()) for _ in range(N)]
a = 0
b = 0
a_b = 0
for s in S:
    if s[-1] == "A" and s[0] == "B":
        a_b += 1
    elif s[-1] == "A":
        a += 1
    elif s[0] == "B":
        b += 1
ans = 0
if a_b > 0:
    ans += a_b - 1
    if a > 0:
        ans += 1
        a -= 1
    if b > 0:
        ans += 1
        b -= 1
ans += min(a, b)
for line in S:
    for i in range(len(line) - 1):
        if (line[i], line[i + 1]) == ("A", "B"):
            ans += 1
print(ans)

N = int(eval(input()))
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = eval(input())
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B":
        B += 1
    elif s[-1] == "A":
        A += 1
if BA == 0:
    ans = cnt + min(A, B)
else:
    ans = cnt + BA - 1
    if A > 0:
        ans += 1
        A -= 1
    if B > 0:
        ans += 1
        B -= 1
    ans += min(A, B)
print(ans)

-S = [eval(input()) for _ in range(N)] -a = 0 -b = 0 -a_b = 0 -for s in S: -    if s[-1] == "A" and s[0] == "B": -        a_b += 1 +cnt = 0 +A = 0 +B = 0 +BA = 0 +for _ in range(N): +    s = eval(input()) +    cnt += s.count("AB") +    if s[0] == "B" and s[-1] == "A": +        BA += 1 +    elif s[0] == "B": +        B += 1 -        a += 1 -    elif s[0] == "B": -        b += 1 -ans = 0 -if a_b > 0: -    ans += a_b - 1 -    if a > 0: +        A += 1 +if BA == 0: +    ans = cnt + min(A, B) +else: +    ans = cnt + BA - 1 +    if A > 0: -        a -= 1 -    if b > 0: +        A -= 1 +    if B > 0: -        b -= 1 -ans += min(a, b) -for line in S: -    for i in range(len(line) - 1): -        if (line[i], line[i + 1]) == ("A", "B"): -            ans += 1 +        B -= 1 +    ans += min(A, B)