main=do
    ctx<-getContents
    let p=tail [msg|msg<- lines ctx]
    let countA = sum $ map istailA p
    let countB = sum $ map isheadB p
    let countAB = sum $ map isthAB p
    let res = sum $ map inLineAB p
    let tailAB= countAB + (if countA>0 then 1 else 0) + (if countB>0 then 1 else 0)
    let tailA= if countA>0 then (countA-1) else countA
    let headB= if countB>0 then (countB-1) else countB
    print $ res+(max 0 (tailAB-1))+(max 0 (min tailA headB))

istailA::[Char]->Int
istailA s=if (last s)=='A' && head s/='B'then 1 else 0

isheadB::[Char]->Int
isheadB s= if (head s)=='B' && last s/='A' then 1 else 0

isthAB::[Char]->Int
isthAB s = if (head s)=='B' && (last s)=='A' then 1 else 0

inLineAB::[Char]->Int
inLineAB s = length [1 |x<-(zip s (drop 1 s)),(snd x)=='B',(fst x) =='A']
