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.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        try (CustomReader in = new CustomReader()) {
            new Main().execute(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void execute(CustomReader in) throws IOException {
        final int N = Integer.parseInt(in.readLine());
        String[] S = new String[N];
        int endWithA = 0;
        int beginWithB = 0;
        int eAbB = 0;
        int ab = 0;
        for (int i = 0; i < N; i++) {
            S[i] = in.readLine();
            if (S[i].charAt(0) == 'B') {
                beginWithB++;
            }
            if (S[i].charAt(S[i].length() - 1) == 'A') {
                endWithA++;
            }
            if (S[i].charAt(0) == 'B' && S[i].charAt(S[i].length() - 1) == 'A') {
                eAbB++;
            }
            for (int j = 0; j < S[i].length() - 1; j++) {
                if (S[i].charAt(j) == 'A' && S[i].charAt(j + 1) == 'B') {
                    ab++;
                }
            }
        }
        int ans = 0;
        if (endWithA > 0 && endWithA == beginWithB && endWithA == eAbB) {
            ans = ab + endWithA - 1;
        } else {
            ans = ab + Math.min(endWithA, beginWithB);
        }

        System.out.println(ans);

    }

    static class CustomReader extends BufferedReader {

        public CustomReader() throws IOException {
            super(new InputStreamReader(System.in));
        }

        public int[] readLineAsIntArray() throws IOException {
            String[] strArray = this.readLine().split(" ");
            int[] intArray = new int[strArray.length];
            for (int i = 0, n = strArray.length; i < n; i++) {
                intArray[i] = Integer.parseInt(strArray[i]);
            }
            return intArray;
        }

        public int[][] readAsIntMatrix(int rows, int columns) throws IOException {
            int[][] matrix = new int[rows][columns];
            for (int i = 0; i < rows; i++) {
                String[] r = this.readLine().split(" ");
                for (int j = 0; j < columns; j++) {
                    matrix[i][j] = Integer.parseInt(r[j]);
                }
            }
            return matrix;
        }

    }
}