stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3369379336088296x):
#include<stdio.h>

#include<cstdio>

#include<iostream>

#include<cmath>

#include<bits/stdc++.h>

using namespace std;





int main()

{

	int i,n;

	cin>>n;

	char ss[111];

	int a,b,y,x;

	a=b=x=y=0;

	while(n--){

		scanf("%s",&ss);

		if(ss[0]=='B'&&ss[strlen(ss)-1]!='A')	b++;

		else if(ss[strlen(ss)-1]=='A'&&ss[0]!='B')	a++;

		else if(ss[0]=='B'&&ss[strlen(ss)-1]=='A')	y++;

		for(i=0;i<strlen(ss);i++){

			if(ss[i]=='A'&&ss[i+1]=='B')

				x++;

		}

	}

	if(y==0) cout<<x+min(a,b)<<endl;

	else if(y!=0&&a==b&&a!=0)	cout<<a+y+x<<endl;

	else if(y!=0&&a==b&&a==0)	cout<<x+y-1<<endl;

	else if(y!=0&&a!=b)	cout<<min(a,b)+x+y<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3278167763663284x):
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;

const int mod=998244353;

int a[1000010];

int main()

{

	int n;

	cin >>n;

	int sum=0;

	int a=0,b=0;

	int cot=0;

	while(n--)

	{

		string s;

		cin >>s;

		int len=s.size();

		if(s[0]=='B'&&s[len-1]=='A') cot++;

		else

		{

			if(s[0]=='B') b++;

			else if(s[len-1]=='A') a++;

		}

		

		for(int i=1;i<len;i++)

		{

			if(s[i]=='B'&&s[i-1]=='A') sum++;

		}

	} 

	if(cot>0)

	{

		if(b==0) sum+=cot-1,b++;

		else sum+=cot;

	 } 

	sum+=min (a,b);

	cout <<sum<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3370784036124022x):
#include <bits/stdc++.h>

using namespace std;

char a[1000];

int main()

{

	long long n,a1=0,a2=0,a3=0,a4=0;

	cin>>n;

	while(n--)

	{

		memset(a,'\0',sizeof(a));

		cin>>a;

			if(a[0]=='B')

			{

				a1++;

			}

			if(a[strlen(a)-1]=='A')

			{

				a2++;

			}

			if(a[strlen(a)-1]=='A'&&a[0]=='B')

			{

				a4++;

			}

		for(int i=0;i<strlen(a)-1;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			{

				a3++;

			}

		}

	}

	long long aa=min(a1,a2);

	if(a1==a4&&a2==a4&&a4!=0)

	{

		a3--;

	}

	cout<<aa+a3<<endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3100834284543896x):
#include <bits/stdc++.h>



using namespace std;



#define all(v)			((v).begin()), ((v).end())

#define SZ(v)			((int)((v).size()))

#define clr(v, d)		memset(v, d, sizeof(v))

#define pb			    push_back



typedef long long ll;



int main() {



	ios_base::sync_with_stdio(false);

	cin.tie(nullptr);

	cout.tie(nullptr);



	int n;

	cin >> n;

	int f = 0, l = 0, f1 = 0;

	int ans = 0;

	for (int i = 0; i < n; ++i) {

		string s;

		cin >> s;

		for (int i = 0; i < SZ(s) - 1; ++i) {

			if (s[i] == 'A' && s[i+1] == 'B')

				ans++;

		}

		if (s[0] == 'B' && s[SZ(s)-1] != 'A')

			f++;

		if (s[0] != 'B' && s[SZ(s)-1] == 'A')

			l++;

		if (s[0] == 'B' && s[SZ(s)-1] == 'A')

			f1++;

	}

	if (f == 0 && l == 0 && f1 > 0)

		ans += f1-1;

	else if (f1 > 0 && (f == 0 || l == 0))

		ans += f1;

	else

		ans += min(f+f1, l+f1);

	cout << ans;

	cout << endl;



	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3404741492645107x):
#include<iostream>

#include<string>

using namespace std;

int main()

{

	int n,len,x=0;

	int nA=0,nB=0,nAB=0;

	cin>>n;

	while(n--)

	{

		string arr;

		cin>>arr;

		len=arr.size();

		for(int i=0;i<len-1;i++)

		{

			if(arr[i]=='A'&&arr[i+1]=='B')

				nAB++;

		}

		if(arr[0]=='B')

			nB++;

		if(arr[len-1]=='A')

			nA++;

		if(arr[0]=='B'&&arr[len-1]!='A'||arr[0]!='B'&&arr[len-1]=='A')

			x=1;

	}

	if(x==0&&nA>0)

		nA--;

	cout<<nAB+min(nA,nB);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3403107348398349x):
#include<iostream>

#include<cstring> 

using namespace std;

int main()

{

	char a[20];

	int n,A=0,B=0,C=0,ans=0;

	cin>>n;

	while(n--)

	{

		cin>>a;

		int len = strlen(a);

		if(a[0]=='B')

		B++;

		if(a[len-1]=='A')

		A++;

		if(a[0]=='B'&&a[len-1]=='A')

		C++;

		for(int i=0;i<len;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			ans++;

		}

	}

	int t;

	if(A>B) 

	t = B;

	else

	t = A;

	if(A == C&&B == C&&C!=0)

		ans--;

	cout<<ans+t;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

typedef long long ll;

string s[11111];

int main(){

	int n,count=0,ba=0,a=0,b=0;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>s[i];

		int len=s[i].size();

		for(int j=0;j<len-1;j++){

			if(s[i][j]=='A'&&s[i][j+1]=='B') count++;

		}

		if(s[i][0]=='B'&&s[i][len-1]=='A') ba++;

		else if(s[i][0]=='B') b++;

		else if(s[i][len-1]=='A') a++;

	}

	int c=min(a,b);

	if(a==0&&b==0&&ba>0) ba--;

	cout<<count+c+ba<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.3835205399428523x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<string>

#include<string.h>

using namespace std;

typedef long long ll;

int main()

{

	int n;

	int l;

	int ant=0,ab=0,a=0,b=0;

	int ans=0;

	scanf("%d",&n);

	for(int i=0;i<n;i++)

	{

		char c[15];

		scanf("%s",c);

//		printf("%s\n",c);

		l=strlen(c);

		for(int j=0;j<l-1;j++)

		{

			if(c[j]=='A'&&c[j+1]=='B')

				ant++;

		}

		if(c[0]=='B'&&c[l-1]=='A')

			ab++;

		else if(c[0]=='B')

			b++;

		else if(c[l-1]=='A')

			a++;

	}

//	printf("ant:%d ab:%d a:%d b:%d\n",ant,ab,a,b);

	ans+=ant;

	if(ab!=0)

		ans+=(ab-1);

	if(a!=0&&ab!=0)

	{

		ans++;

		a--;

	}

	if(b!=0&&ab!=0)

	{

		ans++;

		b--;

	}

	ans+=min(a,b);

	printf("%d\n",ans);

	return 0;

}

/*

5

AB

AB

AB

AB

BB

6

*/
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

typedef long long ll;

string s[11111];

int main(){

	int n,count=0,ba=0,a=0,b=0;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>s[i];

		int len=s[i].size();

		for(int j=0;j<len-1;j++){

			if(s[i][j]=='A'&&s[i][j+1]=='B') count++;

		}

		if(s[i][0]=='B'&&s[i][len-1]=='A') ba++;

		else if(s[i][0]=='B') b++;

		else if(s[i][len-1]=='A') a++;

	}

	int c=min(a,b);

	if(a==0&&b==0&&ba>0) ba--;

	cout<<count+c+ba<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.3468210938385399x):
#include <bits/stdc++.h>

using namespace std;



int main() {

	

	ios::sync_with_stdio(0);

	cin.tie(0), cout.tie(0);

	

	int n; cin >> n;

	int cntA = 0, cntB = 0, cntAB = 0, cnt = 0;

	while (n--) {

		char buf[20]; cin >> buf;

		int len = strlen(buf);

		if (buf[0] == 'B') cntB++;

		if (buf[len - 1] == 'A') cntA++;

		if (buf[0] == 'B' && buf[len - 1] == 'A') cntAB++;

		for (int i = 1; i < len; i++) if (buf[i - 1] == 'A' && buf[i] == 'B') cnt++;

	}

	if (cntA == cntB && cntB == cntAB && cntAB) cnt += cntAB - 1;

	else cnt += min(cntA, cntB);

	cout << cnt << "\n";

	

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int a[3];

	memset(a,0,sizeof(a));

	int cnt = 0;

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') a[0] ++;

		if(s[i][l-1] == 'A') a[1]++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') a[2] ++;

	}

	if(a[0] < a[1])  cnt += a[0];

	else cnt += a[1];

	if(a[0] == a[1] && a[1] == a[2] && a[2] != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.3485592495439205x):
#include <bits/stdc++.h>

using namespace std;



int main() {

	

	ios::sync_with_stdio(0);

	cin.tie(0), cout.tie(0);

	

	int n; cin >> n;

	int cntA = 0, cntB = 0, cntAB = 0, cnt = 0;

	while (n--) {

		char buf[20]; cin >> buf;

		int len = strlen(buf);

		if (buf[0] == 'B') cntB++;

		if (buf[len - 1] == 'A') cntA++;

		if (buf[0] == 'B' && buf[len - 1] == 'A') cntAB++;

		for (int i = 1; i < len; i++) if (buf[i - 1] == 'A' && buf[i] == 'B') cnt++;

	}

	if (cntA == cntB && cntB == cntAB && cntAB) cnt += cntAB - 1;

	else cnt += min(cntA, cntB);

	cout << cnt << "\n";

	

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

typedef long long ll;

string s[11111];

int main(){

	int n,count=0,ba=0,a=0,b=0;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>s[i];

		int len=s[i].size();

		for(int j=0;j<len-1;j++){

			if(s[i][j]=='A'&&s[i][j+1]=='B') count++;

		}

		if(s[i][0]=='B'&&s[i][len-1]=='A') ba++;

		else if(s[i][0]=='B') b++;

		else if(s[i][len-1]=='A') a++;

	}

	int c=min(a,b);

	if(a==0&&b==0&&ba>0) ba--;

	cout<<count+c+ba<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.3644645715490866x):
#include<bits/stdc++.h>

using namespace std;



int main()

{

    char a[15];

    int n;

    cin >> n;

    int b = 0,c = 0,d = 0,e = 0;

    while(n -- )

    {

        memset(a,'\0',sizeof(a));

        cin >> a;

        if(a[0] == 'B')

            b ++ ;

        if(a[strlen(a) - 1] == 'A')

            c ++ ;

        if(a[strlen(a) - 1] == 'A' && a[0] == 'B')

            d ++ ;

        for(int i = 0 ; i < strlen(a) ; i ++ )

        {

            if(a[i] == 'A' && a[i + 1] == 'B')

                e ++ ;

        }

    }

    int aa = min(b,c);

    if(b == d && c == d && d != 0)

        e -- ;

    cout << e + aa << endl;

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int a[3];

	memset(a,0,sizeof(a));

	int cnt = 0;

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') a[0] ++;

		if(s[i][l-1] == 'A') a[1]++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') a[2] ++;

	}

	if(a[0] < a[1])  cnt += a[0];

	else cnt += a[1];

	if(a[0] == a[1] && a[1] == a[2] && a[2] != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.405525767623655x):
#include<bits/stdc++.h>

using namespace std;



int main()

{

    char a[15];

    int n;

    cin >> n;

    int b = 0,c = 0,d = 0,e = 0;

    while(n -- )

    {

        memset(a,'\0',sizeof(a));

        cin >> a;

        if(a[0] == 'B')

            b ++ ;

        if(a[strlen(a) - 1] == 'A')

            c ++ ;

        if(a[strlen(a) - 1] == 'A' && a[0] == 'B')

            d ++ ;

        for(int i = 0 ; i < strlen(a) ; i ++ )

        {

            if(a[i] == 'A' && a[i + 1] == 'B')

                e ++ ;

        }

    }

    int aa = min(b,c);

    if(b == d && c == d && d != 0)

        e -- ;

    cout << e + aa << endl;

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

typedef long long ll;

string s[11111];

int main(){

	int n,count=0,ba=0,a=0,b=0;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>s[i];

		int len=s[i].size();

		for(int j=0;j<len-1;j++){

			if(s[i][j]=='A'&&s[i][j+1]=='B') count++;

		}

		if(s[i][0]=='B'&&s[i][len-1]=='A') ba++;

		else if(s[i][0]=='B') b++;

		else if(s[i][len-1]=='A') a++;

	}

	int c=min(a,b);

	if(a==0&&b==0&&ba>0) ba--;

	cout<<count+c+ba<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.3469924050217739x):
#include<iostream>

#include<cstdio>

#include<map>

#include<algorithm>

#define LL long long



using namespace std;

const int N = 1e4+7;

int main()

{

	ios :: sync_with_stdio(false);

	cin.tie(0);cout.tie(0);

	

	int n;

	cin>>n;

	string s;

	int ans=0,kBA=0,Bk=0,kA=0;

	for(int i=0;i<n;i++){

		cin>>s;

		int len=s.size() ;

		for(int i=1;i<len;i++) {

			if(s[i]=='B'&&s[i-1]=='A') ans++;

		}

		if(s[0]=='B'&&s[len-1]=='A') kBA++;

		else if(s[0]=='B') Bk++;

		else if(s[len-1]=='A') kA++;

	}

	if(kBA>0) {

		ans+=kBA-1;

		if(Bk) ans++,Bk--;

		if(kA) ans++,kA--;

	}

	ans+=min(Bk,kA);

	cout<<ans<<endl;

	return 0;

 }