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.*;
public class Main{
    public static void main(String[] args) throws IOException{
        Reader r = new Reader();      
        int N = r.i();
        int a = 0,
            b = 0,
            c = 0,
        count = 0;
        for(int i = 0; i < N; i++){
            String s = r.s();
            int len = s.length();
            for(int j = 0; j < len-1; j++){
                if(s.charAt(j) == 'A'){
                    if(s.charAt(j+1) == 'B'){
                        count++;
                    }
                }
            }
            int state = 0;            
            if(s.charAt(0) == 'B'){
                state++;
            } 
            if(s.charAt(len-1) == 'A'){
                state += 2;
            }
            switch(state){
                case 1:
                b++;
                break;
                case 2:
                a++;
                break;
                case 3:
                c++;
                break;                    
            }
        }
        if(c > 0){
            count += c-1;
            a++;
            b++;
        }
        count += Math.min(a, b);
        if(c > 0 && a == 1 && b == 1){
            count--;
        }
        print(count);        
    }
    static void print(Object o){
        System.out.println(o.toString());
    }
    static class Reader extends BufferedReader{
        Reader(){
            super(new InputStreamReader(System.in));
        }

        String s() throws IOException{
            return readLine();
        }

        String[] ss() throws IOException{
            return s().split(" ");
        }

        int Int(String s){
            return Integer.parseInt(s);
        }

        int i() throws IOException{
            return Int(s());
        }

        int[] ii() throws IOException{
            String[] ss = ss();
            int size = ss.length;
            int[] ii = new int[size];
            for(int j = 0; j < size; j++) ii[j] = Int(ss[j]);
            return ii;
        }
    }
}