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++14 (GCC 5.4.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, cnt = 0, cntA = 0, cntB = 0, cntAB = 0;
    cin >> n;
    REP(i, n) {
        string s;
        cin >> s;
        if (s.front() == 'B') {
            cntB++;
        }
        if (s.back() == 'A') {
            cntA++;
            if (s.front() == 'B') {
                cntAB++;
            }
        }
        size_t t = 0;
        while (true) {
            t = s.find("AB", t);
            if (t == string::npos) {
                break;
            } else {
                cnt++;
            }
            t += 2;
        }
    }
    int ans = cnt + min(cntA, cntB);
    if (cntA > 0 && cntA == cntB && cntA == cntAB) {
        ans--;
    }
    cout << ans << endl;
    return 0;
}