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.IOException;
import java.io.InputStream;
import java.math.*;

public class Main {

	static FastReaderR in = new FastReaderR(System.in) ;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int N = in.nextInt() ;
		int a = 0 ;
		int b = 0 ;
		int ab = 0 ;
		int ans = 0 ;
		for(int i=0;i<N;i++){
			String line = in.next() ;
			if(line.charAt(0)!='B'&&line.charAt(line.length()-1)!='A'){
				int index = 0 ;
				while(true){
					index = line.indexOf("AB",index) ;
					if(index!=-1)
						ans++ ;
					else
						break ;
					if(index+1>=line.length())
						break ;
					index += 1 ;
				}
			}else{
				if(line.charAt(0)=='B'&&line.charAt(line.length()-1)=='A'){
					ab++ ;
					int index = 0 ;
					while(true){
						index = line.indexOf("AB",index) ;
						if(index!=-1)
							ans++ ;
						else
							break ;
						if(index+1>=line.length())
							break ;
						index += 1 ;
					}
				}else{
					if(line.charAt(0)!='B'&&line.charAt(line.length()-1)=='A'){
						a++ ;
						int index = 0 ;
						while(true){
							index = line.indexOf("AB",index) ;
							if(index!=-1)
								ans++ ;
							else
								break ;
							if(index+1>=line.length())
								break ;
							index += 1 ;
						}
					}
					if(line.charAt(line.length()-1)!='A'&&line.charAt(0)=='B'){
						b++ ;
						int index = 0 ;
						while(true){
							index = line.indexOf("AB",index) ;
							if(index!=-1)
								ans++ ;
							else
								break ;
							if(index+1>=line.length())
								break ;
							index += 1 ;
						}
					}
				}
			}
		}
		ans += Math.min(a, b) ;
		if(a==0&&b==0){
			if(ab!=0)
				ans += (ab-1) ;
		}else{
			if(ab!=0)
				ans += ab ;
		}
		System.out.println(ans);
	}

}

class FastReaderR {
    byte[] buf = new byte[2048];
    int index, total;
    InputStream in;
 
    FastReaderR(InputStream is) {
        in = is;
    }
    int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) {
                return -1;
            }
        }
        return buf[index++];
    }
    int nextInt() throws IOException {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan());
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
    String next() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan());
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }
}



