'use strict'
function main(input) {
  input = input.trim().split('\n')
  input.shift()
  let count = 0
  let firstB = 0
  let lastA = 0
  let bothAB = 0
  input.forEach((val, index) => {
    let match = val.match(/AB/g)
    if (match) count += match.length
    let matchB = val.match(/^B/)
    let matchA = val.match(/A$/)
    if (matchB) firstB++
    if (matchA) lastA++
    if (matchA && matchB) bothAB++
  })
  let min = Math.min(lastA, firstB)
  if (lastA === firstB && lastA === bothAB) min--
  console.log(count + min)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
