{-# LANGUAGE BangPatterns #-}

import Control.Monad
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M

main :: IO ()
main = do
  n <- readLn
  ss <- replicateM n B.getLine
  print $ solve ss

solve :: [B.ByteString] -> Int
solve = (+) <$> sum . map nABSubsters <*> solve' . map shoten

nABSubsters :: B.ByteString -> Int
nABSubsters = length . B.findSubstrings ab
  where ab = B.pack "AB"

shoten :: B.ByteString -> (Char, Char)
shoten s = (h, l)
  where
    h = case B.head s of
      'A' -> 'A'
      'B' -> 'B'
      _   -> 'X'
    l = case B.last s of
      'A' -> 'A'
      'B' -> 'B'
      _   -> 'X'

solve' :: [(Char, Char)] -> Int
solve' = solve'' . count

count :: [(Char, Char)] -> M.Map (Char, Char) Int
count [] = M.fromList
  [ (('A','A'), 0)
  , (('A','B'), 0)
  , (('A','X'), 0)
  , (('B','A'), 0)
  , (('B','B'), 0)
  , (('B','X'), 0)
  , (('X','A'), 0)
  , (('X','B'), 0)
  , (('X','X'), 0)
  ]
count (x:xs) = M.adjust succ x $ count xs

solve'' :: M.Map (Char, Char) Int -> Int
solve'' = go 0 'X'
  where
    go !n 'A' !mp
      | mp M.! ('B', 'A') > 0 = go (n+1) 'A' (M.adjust pred ('B', 'A') mp)
      | mp M.! ('B', 'B') > 0 = go (n+1) 'B' (M.adjust pred ('B', 'B') mp)
      | mp M.! ('B', 'X') > 0 = go (n+1) 'X' (M.adjust pred ('B', 'X') mp)
      | otherwise             = n
    go !n 'B' !mp
      | mp M.! ('X', 'A') > 0 = go n     'A' (M.adjust pred ('X', 'A') mp)
      | mp M.! ('A', 'A') > 0 = go n     'A' (M.adjust pred ('A', 'A') mp)
      | mp M.! ('B', 'A') > 0 = go n     'A' (M.adjust pred ('B', 'A') mp)
      | mp M.! ('X', 'X') > 0 = go n     'X' (M.adjust pred ('X', 'X') mp)
      | mp M.! ('A', 'X') > 0 = go n     'X' (M.adjust pred ('A', 'X') mp)
      | mp M.! ('B', 'X') > 0 = go n     'X' (M.adjust pred ('B', 'X') mp)
      | mp M.! ('X', 'B') > 0 = go n     'B' (M.adjust pred ('X', 'B') mp)
      | mp M.! ('A', 'B') > 0 = go n     'B' (M.adjust pred ('A', 'B') mp)
      | mp M.! ('B', 'B') > 0 = go n     'B' (M.adjust pred ('B', 'B') mp)
      | otherwise             = n
    go !n 'X' !mp
      | mp M.! ('X', 'A') > 0 = go n     'A' (M.adjust pred ('X', 'A') mp)
      | mp M.! ('A', 'A') > 0 = go n     'A' (M.adjust pred ('A', 'A') mp)
      | mp M.! ('B', 'A') > 0 = go n     'A' (M.adjust pred ('B', 'A') mp)
      | mp M.! ('X', 'X') > 0 = go n     'X' (M.adjust pred ('X', 'X') mp)
      | mp M.! ('A', 'X') > 0 = go n     'X' (M.adjust pred ('A', 'X') mp)
      | mp M.! ('B', 'X') > 0 = go n     'X' (M.adjust pred ('B', 'X') mp)
      | mp M.! ('X', 'B') > 0 = go n     'B' (M.adjust pred ('X', 'B') mp)
      | mp M.! ('A', 'B') > 0 = go n     'B' (M.adjust pred ('A', 'B') mp)
      | mp M.! ('B', 'B') > 0 = go n     'B' (M.adjust pred ('B', 'B') mp)
      | otherwise             = n
