from collections import*

n,*s=open(0).read().split()



d=defaultdict(int)

#print(s)

ans=0

for i in s:

    ans+=i.count("AB")

    if i[0]=="B" or i[-1]=="A":

        d[(i[0]=="B",i[-1]=="A")]+=1

#print(d,ans)

if len(list(d.values()))==0:

    print(ans)

    exit()

else:

    if d[(True,True)]>0 and d[(True,False)]>0 and d[(False,True)]>0:  #ABggfgAB

        ans+=d[(True,True)]+1

        d[(True,True)]=0

        d[(True,False)]-=1 

        d[(False,True)]-=1

#        print(d,ans)

    if d[(True,True)]>0 and d[(True,False)]>0:

#        print(d)

        ans+=d[(True,True)]

        d[(True,True)]=0

        d[(True,False)]-=1

    if d[(True,True)]>0 and d[(False,True)]>0:

#        print(d)

        ans+=d[(True,True)]

        d[(True,True)]=0

        d[(False,True)]-=1

    if d[(True,True)]>1:

        ans+=d[(True,True)]-1

    if d[(False,True)]>0 and d[(True,False)]>0:

#        print(d)

        ans+=min(d[(False,True)],d[(True,False)])

print(ans)
n,*t=open(0).read().split()

n=int(n)

a=0

b=0

c=0



ans=0

for i in t:

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

        a+=1

    elif i[0]=="B":

        b+=1

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

        c+=1

    ans+=i.count("AB")

if a>0 and b>0 and c>0:

    ans+=a+1

    a=0

    b-=1

    c-=1

if a>0 and b>0:

    ans+=a

    a=0

    b-=1

if a>0 and c>0:

    ans+=a

    a=0

    c-=1

if a>0:

    ans+=a-1

    a=0

if b>0 and c>0:

    ans+=min(b,c)

print(ans)

from collections import *

n, *s = open(0).read().split()
d = defaultdict(int)
# print(s)
ans = 0
for i in s:
    ans += i.count("AB")
    if i[0] == "B" or i[-1] == "A":
        d[(i[0] == "B", i[-1] == "A")] += 1
# print(d,ans)
if len(list(d.values())) == 0:
    print(ans)
    exit()
else:
    if (
        d[(True, True)] > 0 and d[(True, False)] > 0 and d[(False, True)] > 0
    ):  # ABggfgAB
        ans += d[(True, True)] + 1
        d[(True, True)] = 0
        d[(True, False)] -= 1
        d[(False, True)] -= 1
    #        print(d,ans)
    if d[(True, True)] > 0 and d[(True, False)] > 0:
        #        print(d)
        ans += d[(True, True)]
        d[(True, True)] = 0
        d[(True, False)] -= 1
    if d[(True, True)] > 0 and d[(False, True)] > 0:
        #        print(d)
        ans += d[(True, True)]
        d[(True, True)] = 0
        d[(False, True)] -= 1
    if d[(True, True)] > 1:
        ans += d[(True, True)] - 1
    if d[(False, True)] > 0 and d[(True, False)] > 0:
        #        print(d)
        ans += min(d[(False, True)], d[(True, False)])
print(ans)

n, *t = open(0).read().split()
n = int(n)
a = 0
b = 0
c = 0
ans = 0
for i in t:
    if i[0] == "B" and i[-1] == "A":
        a += 1
    elif i[0] == "B":
        b += 1
    elif i[-1] == "A":
        c += 1
    ans += i.count("AB")
if a > 0 and b > 0 and c > 0:
    ans += a + 1
    a = 0
    b -= 1
    c -= 1
if a > 0 and b > 0:
    ans += a
    a = 0
    b -= 1
if a > 0 and c > 0:
    ans += a
    a = 0
    c -= 1
if a > 0:
    ans += a - 1
    a = 0
if b > 0 and c > 0:
    ans += min(b, c)
print(ans)

-from collections import * - -n, *s = open(0).read().split() -d = defaultdict(int) -# print(s) +n, *t = open(0).read().split() +n = int(n) +a = 0 +b = 0 +c = 0 -for i in s: +for i in t: +    if i[0] == "B" and i[-1] == "A": +        a += 1 +    elif i[0] == "B": +        b += 1 +    elif i[-1] == "A": +        c += 1 -    if i[0] == "B" or i[-1] == "A": -        d[(i[0] == "B", i[-1] == "A")] += 1 -# print(d,ans) -if len(list(d.values())) == 0: -    print(ans) -    exit() -else: -    if ( -        d[(True, True)] > 0 and d[(True, False)] > 0 and d[(False, True)] > 0 -    ):  # ABggfgAB -        ans += d[(True, True)] + 1 -        d[(True, True)] = 0 -        d[(True, False)] -= 1 -        d[(False, True)] -= 1 -    #        print(d,ans) -    if d[(True, True)] > 0 and d[(True, False)] > 0: -        #        print(d) -        ans += d[(True, True)] -        d[(True, True)] = 0 -        d[(True, False)] -= 1 -    if d[(True, True)] > 0 and d[(False, True)] > 0: -        #        print(d) -        ans += d[(True, True)] -        d[(True, True)] = 0 -        d[(False, True)] -= 1 -    if d[(True, True)] > 1: -        ans += d[(True, True)] - 1 -    if d[(False, True)] > 0 and d[(True, False)] > 0: -        #        print(d) -        ans += min(d[(False, True)], d[(True, False)]) +if a > 0 and b > 0 and c > 0: +    ans += a + 1 +    a = 0 +    b -= 1 +    c -= 1 +if a > 0 and b > 0: +    ans += a +    a = 0 +    b -= 1 +if a > 0 and c > 0: +    ans += a +    a = 0 +    c -= 1 +if a > 0: +    ans += a - 1 +    a = 0 +if b > 0 and c > 0: +    ans += min(b, c)