import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import qualified Data.Array.IO as A

main = do
    n <- readLn
    as <- V.replicateM n C.getLine :: IO (V.Vector C.ByteString)
    let innerAB = V.sum $ V.map (countAB 0) as

    let blist = V.map ((=='B') . C.head) as
    let alist = V.map ((=='A') . C.last) as

    let bothlen = V.length $ (V.filter id) $ V.zipWith (&&) blist alist
    let lenBAchain = max 0 (bothlen - 1)

    let alen = V.length (V.filter id alist) - bothlen
    let blen = V.length (V.filter id blist) - bothlen

    let addi = min alen blen
    let chainRemnant = if bothlen>0 && alen+blen > 0 then 1 else 0

    print $ innerAB + lenBAchain + chainRemnant + addi

countAB :: Int -> C.ByteString -> Int
countAB n as = let (l,r) = C.breakSubstring (C.pack "AB") as
                in if C.null r then n else countAB (n+1) (C.tail (C.tail r))
