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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;//list用
using System.Text;//StringBuilder用
using System.Collections;
using System.Linq;
class Program
{
    static List<int> s = new List<int>();
      static List<string> a = new List<string>();
    static decimal result;
static StringBuilder sb = new StringBuilder();
static int N,M,K;
    static void Main()
    {
Read1();
      Read5();
     //中のABを集計
      for(int i=0;i<N;i++){
      for(int j=0;j<a[i].Length-1;j++){
      if(a[i][j]=='A' && a[i][j+1]=='B'){result++;}
      }
      }
      int count,count2,count3;
      count=0;count2=0;count3=0;
      //A末尾Bスタートを集計
      for(int i=0;i<N;i++){
      if(a[i][a[i].Length-1]=='A'){count++;}
        if(a[i][0]=='B'){count2++;}
        if(a[i][a[i].Length-1]=='A' && a[i][0]=='B'){count3++;}
      }
      if(count3==N){result+=N-1;}
      if(count2>=count){result+=count;}else{result+=count2;}
                    Console.WriteLine(result.ToString());

    }


    static void Read1()//1項
    {
        string tmp = Console.ReadLine();
        N = int.Parse(tmp);
    }
 
    static void Read2()//複数項（項目数Ｎ）
    {
        string[] tmp = Console.ReadLine().Split(' ');
        for (int i = 0; i < N; i++) { s.Add(int.Parse(tmp[i])); }
    }
    static void Read3()//1行複数項
   {
       string[] tmp = Console.ReadLine().Split(' ');
       for (int i = 0; i < tmp.Length; i++) { s.Add(int.Parse(tmp[i])); }
    }
    static void Read4()//NK１行
    {
        string[] tmp = Console.ReadLine().Split(' ');
       N = int.Parse(tmp[0]);
       K = int.Parse(tmp[1]);
    }
    static void Read5()//N行K項
    {
for(int i=0;i<N;i++){
a.Add(Console.ReadLine());
}
 
    }


}