from collections import defaultdict, deque, Counter

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



N = int(eval(input()))

A = 0

BA = 0

B = 0

Z = 0

ans = 0



for _ in range(N):

    s = eval(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 BA:

    ans += (BA - 1)

    if A:

        A -= 1

        ans += 1

    if B:

        B -= 1

        ans += 1



ans += min(A, B)

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

A = 0

B = 0

AB = 0

ans = 0

for _ in range(N):

    S = eval(input())

    ans += S.count("AB")

    if S[0] == "B":

        if S[-1] == "A":

            AB += 1

        else:

            B += 1

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

        A += 1



if AB:

    ans += AB-1

    if A:

        ans += 1

        A -= 1

    if B:

        ans += 1

        B -= 1



ans += min(A, B)

print(ans)
from collections import defaultdict, deque, Counter


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


N = int(eval(input()))
A = 0
BA = 0
B = 0
Z = 0
ans = 0
for _ in range(N):
    s = eval(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 BA:
    ans += BA - 1
    if A:
        A -= 1
        ans += 1
    if B:
        B -= 1
        ans += 1
ans += min(A, B)
print(ans)

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

-from collections import defaultdict, deque, Counter - - -def inpl(): -    return list(map(int, input().split())) - - -BA = 0 -Z = 0 +AB = 0 -    s = eval(input()) -    ans += s.count("AB") -    if s[0] == "B": -        if s[-1] == "A": -            BA += 1 +    S = eval(input()) +    ans += S.count("AB") +    if S[0] == "B": +        if S[-1] == "A": +            AB += 1 -    elif s[-1] == "A": +    elif S[-1] == "A": -if BA: -    ans += BA - 1 +if AB: +    ans += AB - 1 +        ans += 1 +    if B: -    if B: -        ans += 1