--{-# LANGUAGE OverloadedStrings #-}
--{-# LANGUAGE OverloadedLists   #-}
{-# LANGUAGE BangPatterns      #-} --引数に!
{-# LANGUAGE MultiWayIf        #-}
{-# LANGUAGE TupleSections     #-}

import Data.Bits
import Data.Foldable (foldlM,foldrM)
import Data.Maybe (fromJust)
import Data.List
import Data.Array  --変数衝突
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import qualified Data.Vector.Unboxed as V
import Control.Monad
import Control.Monad.State
import Control.Monad.ST
import qualified Data.ByteString.Lazy.Char8 as B
--import Debug.Trace

type Ans = Int

cntAB :: String -> Int
cntAB [] = 0
cntAB ('A':'B':str) = 1+(cntAB str)
cntAB (s:str) = cntAB str

cntjoint :: [B.ByteString] -> Int -> Int -> Int -> Int
cntjoint [] a b ba =
  if (a == 0 && b >= 1) || (b == 0 && a >= 1) then ba
    else if a == 0 && b == 0 then max (ba-1) 0
      else ba+1+(min (a-1) (b-1))
cntjoint (bs:bss) a b ba =
  if B.last bs == 'A' && B.head bs == 'B' then cntjoint bss a b (ba+1)
    else if B.last bs == 'A' then cntjoint bss (a+1) b ba
      else if B.head bs == 'B' then cntjoint bss a (b+1) ba
        else cntjoint bss a b ba

solve :: Int -> [B.ByteString] -> Int
solve n bss = (cntjoint bss 0 0 0) + (sum $ map (cntAB. B.unpack) bss)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

main0 :: B.ByteString -> Ans
main0 cont =
  let remLines0 = map B.words (B.lines cont)
      [n']:remLines1 = remLines0
      n = readBInt n'
      bss = take n remLines1
  in solve n (map (\[x] -> x) bss)

main :: IO ()
main = print . main0 =<< B.getContents
