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: Accepted)

#[allow(unused_macros)]
macro_rules! get {
    ([$t:ty] ; $n:expr) => {
        (0..$n)
        .map(|_| get!([$t])).collect::<Vec<_>>()
    };
    ($($t:ty),+ ; $n:expr) => {
        (0..$n).map(|_| get!($($t),+)).collect::<Vec<_>>()
    };
    ([$t:ty]) => {
        rl().split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect::<Vec<$t>>()
    };
    ($t:ty) => {
        rl().parse::<$t>().unwrap()
    };
    ($($t:ty),*) => {
        {
            let line =rl();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap()),*)
        }
    };
}
fn rl() -> String {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).unwrap();
    line.trim().to_string()
}

#[allow(non_snake_case)]
fn main() {
    let n = get!(usize);
    let mut v = Vec::new();
    let mut ans = 0;
    let mut BXA = 0;
    let mut BXX = 0;
    let mut XXA = 0;
    for _ in 0..n {
        let tmp: Vec<char> = get!(String).chars().collect();
        if tmp[0] == 'B' && tmp[tmp.len() - 1] == 'A' {
            BXA += 1;
        } else if tmp[0] == 'B' {
            BXX += 1;
        } else if tmp[tmp.len() - 1] == 'A' {
            XXA += 1;
        }
        for i in 1..tmp.len() {
            if tmp[i - 1] == 'A' && tmp[i] == 'B' {
                ans += 1;
            }
        }
        v.push(tmp);
    }
    //eprintln!("{:?}", v);
    //eprintln!("ans: {}", ans);
    //eprintln!("BXA: {}", BXA);
    //eprintln!("BXX: {}", BXX);
    //eprintln!("XXA: {}", XXA);
    if BXA == 0 {
        ans += std::cmp::min(BXX, XXA);
    } else {
        if BXX == 0 && XXA == 0 {
            ans += BXA - 1;
        } else {
            ans += BXA + std::cmp::min(BXX, XXA);
        }
    }
    println!("{}", ans);
}