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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
public class diverta2019C {
    public static void Main() {
        int N, lastA = 0, firstB = 0, ans = 0;
        string str;
        bool preA = false, same = true, fB = false, lA = false;
        N = int.Parse(System.Console.ReadLine());
        for (int i = 0; i < N; i++) {
            fB = false;
            lA = false;
            preA = false;
            str = System.Console.ReadLine();
            for (int j = 0; j < str.Length; j++) {
                if (str[j] == 'B' && preA) ans++;
                if (str[j] == 'A') preA = true;
                else preA = false;
                if (j == 0 && str[j] == 'B') {
                    firstB++;
                    fB = true;
                }
                if (j == str.Length - 1 && str[j] == 'A') {
                    lastA++;
                    lA = true;
                }
            }
            if ((fB && !lA) || (!fB && lA)) same = false;
        }
        ans += Math.Min(firstB, lastA);
        if (same && ans > 0) ans -= 1;
        System.Console.WriteLine(ans);
    }
}