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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using Debug;
//using static System.Globalization.CultureInfo;
using System.Text;
class Program
{ 
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static void Solve()
    {
        var num = Input.num;
        var ss = Enumerable.Repeat(0, num).Select(_ => ReadLine().Trim()).ToArray();
        int ctf=0, ctl=0,ct=0;
        var ctal = 0;
        for(var i=0;i<num;i++)
        {
            if (ss[i][0] == 'B' && ss[i][ss[i].Length - 1] == 'A') ctal++;
            else
            if (ss[i][0] == 'B') ctl++;
            else
            if (ss[i][ss[i].Length - 1] == 'A') ctf++;
            for (var j = 0; j < ss[i].Length - 1; j++)
                if (ss[i][j] == 'A' && ss[i][j + 1] == 'B') ct++;
        }
        var dp = new int[ctf + 1, ctl + 1, ctal + 1];
        for (var i = 0; i <= ctf; i++)
            for (var j = 0; j <= ctl; j++)
                for (var h = 0; h <= ctal; h++)
                {
                    if (i > 0 && j > 0)
                        dp[i, j, h] = dp[i - 1, j - 1, h] + 1;
                    if (i > 0 && j > 0 && h > 0)
                        dp[i, j, h] = Max(dp[i, j, h], dp[i - 1, j - 1, h - 1] + 2);
                    if (j > 0 && h > 1)
                        dp[i, j, h] = Max(dp[i, j, h], dp[i, j - 1, h - 2] + 2);
                    if (h == ctal) dp[i, j, h] = Max(dp[i, j, h], dp[i, j, 0] + Max(0, ctal - 1));
                    if (i > 0 && h > 0)
                        dp[i, j, h] = Max(dp[i, j, h], dp[i - 1, j, 0] + h);
                    if (j > 0 && h > 0)
                        dp[i, j, h] = Max(dp[i, j, h], dp[i, j - 1, 0] + h);
                }
        WriteLine(dp[ctf,ctl,ctal] + ct);
        /*var r = 0;
        if (ctf == ctl) r += ctf + ctal;
        else if (Abs(ctl - ctf) <= ctal) r += Max(ctf, ctl) + (ctal - Abs(ctl - ctf)) / 2;
        else r = Min(ctf, ctl) + ctal;
        WriteLine(r+ct);*/
    }
}

public class Input
{
    public static string read => ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public  static int num => ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(_ => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(_ => arL).ToArray();
    public static T getValue<T>(string g)
    {
        var t = typeof(T);
        if (t == typeof(int))
            return (T)(object)int.Parse(g);
        if (t == typeof(long))
            return (T)(object)long.Parse(g);
        if (t == typeof(string))
            return (T)(object)g;
        if (t == typeof(char))
            return (T)(object)char.Parse(g);
        if (t == typeof(double))
            return (T)(object)double.Parse(g);
        if (t == typeof(bool))
            return (T)(object)bool.Parse(g);
        return default(T);
    }
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}