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 Julia (0.5.0) (judge verdict: Accepted)

parseInt(x) = parse(Int, x)

function main()
  N = readline() |> parseInt
  s = Array{String}(N)
  for i=1:N
    s[i]=readline()|>strip
  end

  # w=count(b->b,map(ss->contains(ss,"AB"),s))
  w=sum(map(ss->(split(ss,"AB")|>length)-1,s))
  x=count(b->b,map(ss->ss[1]!='B'&&ss[end]=='A',s))
  y=count(b->b,map(ss->ss[1]=='B'&&ss[end]!='A',s))
  z=count(b->b,map(ss->ss[1]=='B'&&ss[end]=='A',s))
  # println([x,y,z,w])
  # result=z-1+(x>=1?1:0)+(y>=1?1:0)+((x>=1&&y>=1)?min(x-1,y-1):0)+w
  result=if z==0
    min(x,y)
  elseif x+y>0
    z+min(x,y)
  else
    z-1
  end
  result+=w

  println(result)
end

main()