Problem:
Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! invec {
    ( $t:ty ) => {{
        let mut s = String::new();
        match std::io::stdin().read_line(&mut s) {
            Ok(0) => Vec::<$t>::new(),
            Ok(n) => s.trim().split_whitespace().map(|s| s.parse::<$t>().unwrap()).collect::<Vec<$t>>(),
            Err(_) => Vec::<$t>::new(),
        }
    }}
}

#[allow(unused_macros)]
macro_rules! input {
    ( $($t:ty),* ) => {{
        let mut s = String::new();
        std::io::stdin().read_line(&mut s);
        let mut splits = s.trim().split_whitespace();
        ($(
            {
                 splits.next().unwrap().parse::<$t>().unwrap()
            },
        )*)
    }}
}

fn count_string(target: &str, s: &str) -> u32 {
    match target.find(s) {
        Some(n) => return 1 + count_string(&target[(n+1)..target.len()], s),
        None => 0,
    }
}

#[allow(unused_must_use)]
#[allow(unused_variables)]
fn solve() {
    let (n,) = input!(u64);
    let mut ss = vec![];

    for _ in 0..n {
        let (s,) = input!(String);
        ss.push(s);
    }

    let mut count = 0;
    let mut count_first_b = 0;
    let mut count_last_a = 0;
    let mut count_both = 0;
    for s in ss {
        count += count_string(&s, "AB");
        if &s[0..1] == "B" && &s[(s.len()-1)..s.len()] == "A" {
            count_both += 1;
        } else if &s[0..1] == "B" {
            count_first_b += 1;
        } else if &s[(s.len()-1)..s.len()] == "A" {
            count_last_a += 1;
        }
    }

    if count_first_b == 0 && count_last_a == 0 {
        count += count_both - 1;
    } else {
        count += count_both + std::cmp::min(count_first_b, count_last_a);
    }
    
    println!("{}", count);
}

fn main() {
    solve();
}