#include<iostream>
#include<string>
#include<map>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
string maps[252];
map<char,int> M;
int a[5];//记录每一个福娃在两行相同的位子出现的次数
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int text;
cin>>text;
M['B'] = 0;//将福娃与编号绑定
M['J'] = 1;
M['H'] = 2;
M['Y'] = 3;
M['N'] = 4;
while(text--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int n,m;
int i,j,k;
cin>>n>>m;
int ans = 0;
for(i = 1;i <= n ;i++)
cin>>maps[i];
for(i = 1;i <= n;i++)//从第i行
for(j = i+1; j <= n;j++)//到第j行
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
memset(a,0,sizeof(a));
for(k = 0;k < m;k++)//相同的k列是否是相同的福娃
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(maps[i][k] == maps[j][k])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ans = ans + a[M[maps[i][k]]];//记录总的个数,先加以前的个数
a[M[maps[i][k]]] ++;//再将自身加一,很巧妙
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
cout<<ans<<endl;
}
return 0;
}