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.*;

class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int N = Integer.parseInt(str);
        String[] s = new String[N];
        for (int i=0;i<N;i++){
            s[i] = br.readLine();
        }
        int ans = 0;
        int cnt_AB = 0;
        int cnt_A = 0;
        int cnt_B = 0;
        for (String ss:s){
            for (int i=1;i<ss.length();i++){
                if (String.valueOf(ss.charAt(i-1)).equals("A") && String.valueOf(ss.charAt(i)).equals("B")){
                    ans++;
                }
            }
            if (String.valueOf(ss.charAt(0)).equals("B") && String.valueOf(ss.charAt(ss.length()-1)).equals("A")){
                cnt_AB++;
            } else if (String.valueOf(ss.charAt(0)).equals("B")){
                cnt_B++;
            } else if (String.valueOf(ss.charAt(ss.length()-1)).equals("A")){
                cnt_A++;
            }
        }
        if (cnt_A==0 && cnt_B==0){
            if (cnt_AB!=0){
                ans += cnt_AB-1;
            }
        } else {
            ans += cnt_AB+Math.min(cnt_A,cnt_B);
        }
        System.out.println(ans);
    }
}