object Main extends App {
  val in = scala.io.Source.stdin.getLines.toSeq
  val ls = in.tail
  val sub = ls.foldLeft(0L){ (a,e) =>
    val tmp = (0 until e.size-1).count(j => (e(j)=='A' && e(j+1)=='B'))
    //println(tmp)
    a + tmp
  }
  val fb = ls.count(_.head == 'B')
  val la = ls.count(_.last == 'A')
  val ba = ls.count(e => e.head == 'B' && e.last == 'A')
  val adj = if(ba == fb && ba == la) 1 else 0
  val ret = sub + Math.min(fb, la) - adj
  println(ret)
}
