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)

n = gets.to_i
ans = 0
tmp1 = [] # A starts
tmp2 = [] # B ends
tmp3 = [] # A starts and B ends
n.times do
  s = gets.chomp
  s.chars.each.with_index do |c,i|
    break if s[i+1].nil?
    if c == "A" && s[i+1] == "B"
      ans += 1
    end
  end

  if s[-1] == "A" && s[0] == "B"
    tmp3 << s
  elsif s[-1] == "A"
    tmp1 << s
  elsif s[0] == "B"
    tmp2 << s
  end
end

acnt = tmp1.size
bcnt = tmp2.size
min = [acnt, bcnt].min
acnt -= min
bcnt -= min
ans += min

ccnt = tmp3.size

# if ccnt >= 2
#   ans += ccnt - 1
#   ccnt = 1
# end

if acnt > bcnt
  cnt = acnt
elsif bcnt > acnt
  cnt = bcnt
elsif acnt.zero? && bcnt.zero?
  cnt = 0
end

if cnt.zero?
  ans += ccnt - 1 if ccnt > 0
else
  if ccnt >= cnt
    ans += cnt
    ccnt -= cnt
    ans += ccnt - 1 if ccnt > 0
  else
    ans += cnt
    ccnt -= cnt
    ans += ccnt - 1 if ccnt > 0
  end
end

puts ans