import Control.Monad
import Data.List

main = do
    n <- readLn
    ss <- replicateM n getLine
    let ab = sum $ map  (length . (filter ("AB" `isPrefixOf`)) . tails) ss
        a = length $ filter ((=='A') . last) ss
        b = length $ filter ((=='B') . head) ss
        ba = length $ filter (\x -> last x == 'A' && head x == 'B') ss
    print $ ab + min a b - if a == b && ba > 0 then 1 else 0