import Control.Monad

readIntLine :: IO ([Int])
readIntLine = do
    x <- getLine
    return $ map (read :: String -> Int) $ words x

main :: IO ()
main = do
    [n] <- readIntLine
    lines <- replicateM n getLine
    solve lines

solve lines =
    let inner = sum $ map countAB lines
        outer1 = (length $ filter (('A'==). last) lines)
        outer2 = (length $ filter (('B'==). head) lines)
        outer = min outer1 outer2
        yonder = if (length $ filter (\l -> (('A'==). last) l && (('B'==). head) l) lines) == (max outer1 outer2) && outer > 0 then 1
                                                                                                                               else 0
      in print $ inner + outer - yonder

countAB [] = 0
countAB [x] = 0
countAB ('A':'B':xs) = 1 + countAB xs
countAB (x:xs) = countAB xs
