/**
Author : Kamonasish Roy (Bullet)
Key idea:
First we need to store each position frequancy.
Then 1 to n, we check i and i+1, max frequency character for each position.
Just combine and find maximum possible answer.
**/
#include<bits/stdc++.h>
using namespace std;
const long long M=4e5,MOD=1e9+7;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vector<string>p;
vector<vector<int>>occ(m+1,vector<int>(26,0));
vector<vector<int>>occ1(m+1,vector<int>(26,0));
for(int i=1;i<=n;i++){
string s;
cin>>s;
p.push_back(s);
for(int j=0;j<m;j++){
occ[j][s[j]-'a']++;
}
}
int best=0;
for(int i=n-1;i>=0;i--){
int cur_best=0;
for(int j=0;j<m;j++){
int L=0;
int R=0;
for(int k=0;k<26;k++)L=max(L,occ[j][k]),R=max(R,occ1[j][k]);
cur_best+=L+R;
}
best=max(best,cur_best);
for(int j=0;j<m;j++){
char ch=p[i][j];
occ[j][ch-'a']--;
occ1[j][ch-'a']++;
}
}
cout<<best<<"\n";
}
return 0;
}