n = int(eval(input()))

S = [eval(input()) for i in range(n)]



ans = 0

start_b = 0

end_a = 0

ab = 0



for i in range(n):

    for j in range(len(S) - 1):

        if S[i][j:j+2] == 'AB':

            ans += 1



    if S[i][0] == 'B' and S[i][-1] == 'A':

        ab += 1

        continue 

    if S[i][0] == 'B':

        start_b += 1

    if S[i][-1] == 'A':

        end_a += 1

    



if ab > 0:

    ans += ab - 1

    if start_b > 0:

        ans += 1

        start_b -= 1

    if end_a > 0:

        ans += 1

        end_a -= 1

        

ans += min(start_b, end_a)



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

S = [eval(input()) for i in range(n)]



ans = 0

start_b = 0

end_a = 0

ab = 0



for i in range(n):

    for j in range(len(S[i]) - 1):

        if S[i][j:j+2] == 'AB':

            ans += 1



    if S[i][0] == 'B' and S[i][-1] == 'A':

        ab += 1

        continue 

    if S[i][0] == 'B':

        start_b += 1

    if S[i][-1] == 'A':

        end_a += 1

    



if ab > 0:

    ans += ab - 1

    if start_b > 0:

        ans += 1

        start_b -= 1

    if end_a > 0:

        ans += 1

        end_a -= 1

        

ans += min(start_b, end_a)



print(ans)
n = int(eval(input()))
S = [eval(input()) for i in range(n)]
ans = 0
start_b = 0
end_a = 0
ab = 0
for i in range(n):
    for j in range(len(S) - 1):
        if S[i][j : j + 2] == "AB":
            ans += 1
    if S[i][0] == "B" and S[i][-1] == "A":
        ab += 1
        continue
    if S[i][0] == "B":
        start_b += 1
    if S[i][-1] == "A":
        end_a += 1
if ab > 0:
    ans += ab - 1
    if start_b > 0:
        ans += 1
        start_b -= 1
    if end_a > 0:
        ans += 1
        end_a -= 1
ans += min(start_b, end_a)
print(ans)

n = int(eval(input()))
S = [eval(input()) for i in range(n)]
ans = 0
start_b = 0
end_a = 0
ab = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j : j + 2] == "AB":
            ans += 1
    if S[i][0] == "B" and S[i][-1] == "A":
        ab += 1
        continue
    if S[i][0] == "B":
        start_b += 1
    if S[i][-1] == "A":
        end_a += 1
if ab > 0:
    ans += ab - 1
    if start_b > 0:
        ans += 1
        start_b -= 1
    if end_a > 0:
        ans += 1
        end_a -= 1
ans += min(start_b, end_a)
print(ans)

-    for j in range(len(S) - 1): +    for j in range(len(S[i]) - 1):