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 JavaScript (node.js v5.12) (judge verdict: Wrong Answer)

"use strict";
function I(t,i){this._s=t.split("\n"),this._c=0,this._l=0,this._f=i||Number}I.prototype.a=function(t){var i=this._s[this._l];if(i){var s=i.trim().split(" "),h=s[this._c];return this._c++,s.length===this._c&&(this._c=0,this._l++),t?t(h):this._f(h)}},I.prototype.l=function(t){var i=this._s[this._l];if(i)return this._c=0,this._l++,i.split(" ").map(t||this._f)},module.exports=I;

function main(input) {
  var o = new I(input);
  var n = o.a();
  var s = [];
  for (var i = 0; i < n; i++) {
    s.push(o.a(String));
  }

  var a = 0, b = 0, c = 0;
  for (var i = 0; i < n; i++) {
    if (s[i][0] === "B" && s[i][s[i].length - 1] === "A") {
      c += 1;
    } else if (s[i][0] === "B") {
      b += 1;
    } else if (s[i][s[i].length - 1] === "A") {
      a += 1;
    }
  }

  var base = 0;
  s.forEach(x => {
    for (var i = 0; i < x.length - 1; i++) {
      if (x.slice(i, i + 2) === "AB") {
        base += 1;
      }
    }
  });

  // console.log({a, b, c, base})

  console.log(base + Math.min(a, b) + c);
}


main(require("fs").readFileSync("/dev/stdin", "utf8"));