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.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(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, FastScanner in, PrintWriter out) {
            int N = in.ni();
            int ba = 0, xa = 0, bx = 0;
            int ans = 0;
            for (int i = 0; i < N; i++) {
                String s = in.ns();
                for (int j = 1; j < s.length(); j++) {
                    if (s.charAt(j - 1) == 'A' && s.charAt(j) == 'B')
                        ans++;
                }
                int n = s.length();
                char first = s.charAt(0);
                char last = s.charAt(n - 1);
                if (first == 'B' && last == 'A') {
                    ba++;
                } else if (first == 'B') {
                    bx++;
                } else if (last == 'A') {
                    xa++;
                }
            }

            if (ba > 0) {
                ans += ba - 1;
                ba = 1;
            }
            if (ba > 0 && xa > 0 && bx > 0) {
                ba = 0;
                xa--;
                bx--;
                ans += 2;
            }
            int cnt = Math.min(xa, bx);
            ans += cnt;
            xa -= cnt;
            bx -= cnt;
            if (ba > 0 && (xa > 0 || bx > 0)) {
                ans++;
            }
            out.println(ans);
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String ns() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String rl = in.readLine();
                    if (rl == null) {
                        return null;
                    }
                    st = new StringTokenizer(rl);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(ns());
        }

    }
}

