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.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	private int countAB(String s) {
		int idx = 0;
		int result = 0;
		for (;;) {
			int t = s.indexOf("AB", idx);
			if (t >= 0) {
				result++;
				idx = t + 1;
			} else {
				break;
			}
		}
		return result;
	}

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();

		int countAB = 0;
		int A = 0;
		int B = 0;
		int BA = 0;

		for (int i = 0; i < N; i++) {
			String s = sc.next();

			countAB += countAB(s);

			if (s.startsWith("B") && s.endsWith("A")) {
				BA++;
			} else if (s.startsWith("B")) {
				B++;
			} else if (s.endsWith("A")) {
				A++;
			}
		}

		int ans = 0;

		int state = 0;
		for (;;) {
			switch (state) {
			case 0: //start
				if (A > 0) {
					A--;
					state = 1; //A
				} else if (BA > 0) {
					BA--;
					state = 1; //A
				} else {
					state = 3; //end
				}
				break;
			case 1: //A
				if (BA > 0) {
					BA--;
					state = 1;
					ans++;
				} else if (B > 0) {
					B--;
					state = 2; //x
					ans++;
				} else {
					state = 3;
				}
				break;
			case 2: //B
				if (A > 0) {
					A--;
					state = 1;
				} else {
					state = 3;
				}
				break;
			}
			if (state == 3) {
				break;
			}
		}

		out.println(ans + countAB);

		sc.close();

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

}
