import re

n=int(eval(input()))

ans=0

a,b,ba=0,0,0

for i in range(n):

  s=eval(input())

  ans+=len(re.sub("[A-Z]+","",s.replace("AB","s")))

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

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

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

ans+=max(ba-1,0)

if ba:

  if a:ans+=1;a-=1

  if b:ans+=1;b-=1

print((ans+min(a,b)))
n=int(eval(input()))

l=[0,0,0,0]#non,~a,b~,b~a

ans=0

for i in range(n):

    s=eval(input())

    ans+=s.count("AB")

    l[2*int(s[0]=="B")+1*int(s[-1]=="A")]+=1



if l[-1]:

    ans+=l[-1]-1

    if l[1]:

        l[1]-=1

        ans+=1

    if l[2]:

        l[2]-=1

        ans+=1

print((ans+min(l[1:3])))
import re

n = int(eval(input()))
ans = 0
a, b, ba = 0, 0, 0
for i in range(n):
    s = eval(input())
    ans += len(re.sub("[A-Z]+", "", s.replace("AB", "s")))
    if s[0] == "B" and s[-1] == "A":
        ba += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1
ans += max(ba - 1, 0)
if ba:
    if a:
        ans += 1
        a -= 1
    if b:
        ans += 1
        b -= 1
print((ans + min(a, b)))

n = int(eval(input()))
l = [0, 0, 0, 0]  # non,~a,b~,b~a
ans = 0
for i in range(n):
    s = eval(input())
    ans += s.count("AB")
    l[2 * int(s[0] == "B") + 1 * int(s[-1] == "A")] += 1
if l[-1]:
    ans += l[-1] - 1
    if l[1]:
        l[1] -= 1
        ans += 1
    if l[2]:
        l[2] -= 1
        ans += 1
print((ans + min(l[1:3])))

-import re - +l = [0, 0, 0, 0]  # non,~a,b~,b~a -a, b, ba = 0, 0, 0 -    ans += len(re.sub("[A-Z]+", "", s.replace("AB", "s"))) -    if s[0] == "B" and s[-1] == "A": -        ba += 1 -    elif s[0] == "B": -        b += 1 -    elif s[-1] == "A": -        a += 1 -ans += max(ba - 1, 0) -if ba: -    if a: +    ans += s.count("AB") +    l[2 * int(s[0] == "B") + 1 * int(s[-1] == "A")] += 1 +if l[-1]: +    ans += l[-1] - 1 +    if l[1]: +        l[1] -= 1 -        a -= 1 -    if b: +    if l[2]: +        l[2] -= 1 -        b -= 1 -print((ans + min(a, b))) +print((ans + min(l[1:3])))