open Printf open Scanf
open Array

let print_list f ls = List.iter (fun v -> printf "%s " @@ f v) ls; print_newline ()

(* rosetta code *)
let count_substring str sub =
  let sub_len = String.length sub in
  let len_diff = (String.length str) - sub_len
  and reg = Str.regexp_string sub in
  let rec aux i n =
    if i > len_diff then n else
      try
        let pos = Str.search_forward reg str i in
        aux (pos + sub_len) (succ n)
      with Not_found -> n
  in
  aux 0 0

let () = scanf " %d" @@ fun n ->
  let arr = init n (fun _ -> scanf " %s" @@ fun s -> s)
  in
  let kba,kbx,kxa,krest = fold_left (fun (kba,kbx,kxa,krest) s ->
    let bx = s.[0] = 'B' in
    let xa = s.[String.length s-1] = 'A' in
    let ab = bx && xa in
    let kba = if ab then kba + 1 else kba in
    let kbx = if not ab then (
      if bx then kbx+1 else kbx
    ) else kbx in
    let kxa = if not ab then (
      if xa then kxa+1 else kxa
    ) else kxa in
    let krest = krest + count_substring s "AB"
    in
    (kba,kbx,kxa,krest)
  ) (0,0,0,0) arr in
  (* printf "%d %d %d %d\n" kba kbx kxa krest; *)
  let pair_ab = min kxa kbx in
  (* -AB- ... -AB- *)
  let hoge = if kba >= 1 then (
    (* ^B-A^ ^B-A^ ... ^B-A^ *)
    let pair_ba = kba - 1 in
    if pair_ab > 0 then (
      (* ^-A^B-^ ... ^-A^B-^ + ^BA^BA^...^BA^ *)
      let rkxa,rkbx = kxa - pair_ab, kbx - pair_ab in
      if rkxa > 0 || rkbx > 0 then (
        pair_ab + pair_ba + 1
      ) else pair_ab + pair_ba
    ) else if kxa > 0 || kbx > 0 then (
      (* -A + BA^BA or BA^BA + B-*)
      pair_ba + 1
    ) else (
      (* BA ... BA *)
      pair_ba
    )
  ) else (
    (* -AB- ... -AB- *)
    pair_ab
  ) in
  print_int @@ krest + hoge


  (* let pair_ab = min kxa kbx in
  let kxa,kbx = kxa - pair_ab, kbx - pair_ab in
  let pair_ba = max 0 @@ kba - 1 in (* BA...BA *)
  printf "%d %d %d %d\n"
    pair_ab kxa kbx kba;
  let hoge =
    if pair_ab = 0 then
      if kxa > 0 || kbx > 0 then
        kba
      else pair_ba
    else (
      pair_ab +
        if kba = 0 then 0
        else if kxa > 0 || kbx > 0 then
          kba + 1
        else kba
    )
  in print_int @@ krest + hoge *)

  (*
  let kba = kba - pair_ba in
  if kba > 0 && (kxa > 0 || kbx > 0) then (

  ) *)
  (* print_int @@ krest + pair_ab + pair_ba +
    if kxa > 0 || kbx > 0 then
      if kba = 0 then 0 else 1
    else 0 *)




