
import Data.Bool
import Debug.Trace
import Data.List
import Control.Monad
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import qualified Data.Array.IO as A

rVI :: Int -> IO (U.Vector Int)
rVI n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r :: IO [Int]
r = map read . words <$> getLine

main = do
    n <- readLn
    as <- V.replicateM n C.getLine :: IO (V.Vector C.ByteString)
    let r = 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
    --print (alen, blen, bothlen)

    let addi = min alen blen
    let chainRemnant = if alen-blen==0 then 0 else 1  

    --print (r,addi)
    print $ r + lenBAchain + addi + chainRemnant

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))
