Problem:
Score : 400 points

Problem Statement

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.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
char str[22];
int n,cnta,cntb,ans;
int main() {
	rd(n);
	for(int i=1;i<=n;++i) {
		scanf("%s",str+1); int len=strlen(str+1);
		for(int i=1;i<len;++i) ans+=str[i]=='A'&&str[i+1]=='B';
		int flga=0,flgb=0;
		if(str[1]=='B') {
			if(cnta) cnta--,ans++,flgb=1;
		}
		if(str[len]=='A') {
			if(cntb) cntb--,ans++,flga=1;
		}
		if(str[1]=='B') {
			if(!flgb) cntb++;
		}
		if(str[len]=='A') {
			if(!flga) cnta++;
		}
	}
	printf("%d\n",ans);
	return 0;
}