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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

int main() {
	LL n; cin >> n;

	vector<string> a(n);

	LL ans = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}


	LL cnta = 0;
	LL cntb = 0;
	LL cntc = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < a[i].size()-1; j++) {
			if (a[i][j] == 'A' && a[i][j + 1] == 'B')ans++;
		}

		if (a[i][a[i].size() - 1] == 'A' && a[i][0] == 'B')cntc++;
		else if (a[i][a[i].size() - 1] == 'A')cnta++;
		else if (a[i][0] == 'B')cntb++;
	}

	if (cnta > 0 && cntb > 0) {
		ans += min(cnta, cntb);
		
		if (cnta > cntb) {
			cnta -= cntb;
			cntb = 0;
		}
		else if (cnta == cntb) {
			cnta = 0;
			cntb = 0;
		}
		else {
			cntb -= cnta;
			cnta = 0;
		}
	}
	
	if (cnta == 0 && cntb>0) {
		ans += cntc;
		cout << ans << endl;
		return 0;
	}
	
	if (cntb == 0 && cnta > 0) {
		ans += cntc;
		cout << ans << endl;
		return 0;
	}
	
	if (cntb == 0 && cnta == 0) {
		if (cntc > 0) {
			ans = ans + (cntc - 1);
		}
		
		cout << ans << endl;
		return 0;
	}

	

	return 0;
}