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.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

    // input
    static int N;
    static String[] s;

    /**
     * input
     */
    static void input(){
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        s = new String[N];
        for(int i=0;i<N;i++){
            s[i] = sc.next();
        }
        sc.close();
    }
 
    /**
     * solver
     */
    static void solve(){
        int ans;
        int ab = 0;
        int ba = 0;
        int a=0;
        int b=0;

        for(int i=0;i<N;i++){
            ab += s[i].split("AB", -1).length - 1;
            boolean _b = false;
            boolean _a = false;

            if(s[i].charAt(0) == 'B'){
                _b = true;
            }
            if(s[i].charAt(s[i].length() - 1) == 'A') {
                _a = true;
            }

            if (_a && _b) ba ++;
            if (_a && !_b) a ++;
            if (!_a && _b) b ++;
        }

        if (ba > 0) {
            ans = ab + ba - 1;
            if (a>0) {
                ans ++;
                a --;
            }
            if (b>0) {
                ans ++;
                b --;
            }
            ans += Math.min(a,b);
        } else {
            ans = ab + Math.min(a,b);
        }

        System.out.println(ans);

    }

    public static void main(String args[]) {

        input();

        solve();
    }
}
