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.

Examples

Input

3
ABCA
XBAZ
BAD


Output

2


Input

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA


Output

4


Input

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA


Output

4
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CABSubstrings solver = new CABSubstrings();
        solver.solve(1, in, out);
        out.close();
    }

    static class CABSubstrings {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int cntAB = 0;
            int finA = 0;
            int staB = 0;
            int both = 0;
            for (int i = 0; i < n; i++) {
                String s = in.next();
                if (s.startsWith("B") && s.endsWith("A")) both++;
                else if (s.startsWith("B")) staB++;
                else if (s.endsWith("A")) finA++;
                cntAB += f(s);
            }
            out.println(staB + finA > 0 ? cntAB + Math.min(staB, finA) + both : cntAB + Math.max(both - 1, 0));
        }

        int f(String s) {
            // "AB"を数える
            int res = 0;
            for (int i = 0; i < s.length() - 1; i++) {
                if (s.charAt(i) == 'A' && s.charAt(i + 1) == 'B') res++;
            }
            return res;
        }

    }
}

