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 Ruby (2.3.3) (judge verdict: Wrong Answer)

def find_ab(str)
    return str.include?('A') || str.include?('B')
end

count = 0
N = (gets).to_i
strs = []
N.times do
    strs << gets.strip.gsub(/[^AB]+/, "-")
end
#puts strs

strs.map! { |str| str.gsub(/AB/) { count += 1; "" } }
#puts strs

strs.select! { |a| (a.length > 0) && find_ab(a) }
strs.sort!

# 別に律義にくっつけてみる必要もなく、先頭と末尾を見れば
# 何個できそうかわかる
n_b_ = 0
n__a = 0
n_b_a = 0
strs.each do |str|
    if str=~/^B.*A$/ then
        n_b_a += 1
    elsif str=~/^B/ then
        n_b_ += 1
    elsif str=~/A$/ then
        n__a += 1
    end
end

ct = [n_b_, n__a].min
count += ct
n_b_ -= ct
n__a -= ct

1.upto(n_b_a) do |nba|
    f = false
    if (n_b_ > 0) then
        count += 1
        n_b_ -= 1
    elsif (nba < n_b_a) then
        #n_b_a -= 1
        f = true
        count += 1
    end
    if (n__a > 0) then
        count += 1
        n__a -= 1
    elsif (nba < n_b_a) && !f then
        #n_b_a -= 1
        count += 1
    end
end

puts count