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 Python (3.4.3) (judge verdict: Wrong Answer)

def c_ab_substrings(N, S):
    """
    N個の文字列を好きな順序で並べたあと、連結して1つの文字列を作る。
    作った文字列に 'AB' という部分文字列が含まれる個数として
    ありうる値のうち最大のものを求めよ。
    """

    num_start_b_end_a = 0
    num_start_b_not_end_a = 0
    num_not_start_b_end_a = 0
    ans = 0
    for s in S:
        if s[0] == 'B' and s[-1] == 'A':
            num_start_b_end_a += 1
        elif s[0] == 'B' and s[-1] != 'A':
            num_start_b_not_end_a += 1
        elif s[0] != 'B' and s[-1] == 'A':
            num_not_start_b_end_a += 1
        if 'AB' in s:
            ans += 1
    if num_start_b_end_a == 0:
        # 'B.*A' がない場合、'B.*^A' と '^B.*A' を結合して '.*AB.*' を生み出せる
        ans += min(num_start_b_not_end_a, num_not_start_b_end_a)
    else:
        # 'B.*A' がある場合、その後に 'B.*^A' を結合するか、その前に '^B.*A' を結合する
        # 'B.*^A' も '^B.*A' もない場合、 'B.*A' 同士で結合させる
        if num_start_b_not_end_a + num_not_start_b_end_a > 0:
            ans += num_start_b_end_a + min(num_start_b_not_end_a, num_not_start_b_end_a)
        else:
            ans += num_start_b_end_a - 1
    return ans

N = int(input())
S = [input() for _ in range(N)]
print(c_ab_substrings(N, S))