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 blen = V.length $ V.filter (=='B') (V.map (C.head) as)
    let alen = V.length $ V.filter (=='A') (V.map (C.last) as)
    let addi = min blen alen
    print $ r + 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))