#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define pi 2*acos(0.0)
#define f first
#define s second
#define inf INT_MAX
#define MXN 500001
#define endl "\n"
#define eps 1e-6
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rev(x) x.rbegin(), x.rend()
#define spc(x) cout << fixed << setprecision(x)
#define fastio {ios_base::sync_with_stdio(false); cin.tie(NULL);}
#define test ll t; cin >> t; while (t--)
typedef long long ll;
using namespace std;
void solve()
{
ll n,m;
cin>>n>>m;
vector<string> v;
for(int i=0;i<n;i++){
string s; cin>>s;
v.pb(s);
}
ll ans=0;
map<ll,vector<char>> mp,mpp;
for(int i=0;i<n;i++){
string ss = v[i];
for(int j=0;j<m;j++){
mp[j].pb(ss[j]);
if(i<n-1){
mpp[j].pb(ss[j]);
}
}
}
for(int i=0;i<m;i++){
map<char,ll> mp1,mpp1;
for(int j=0;j<mp[i].size();j++){
mp1[mp[i][j]]++;
}
for(int j=0;j<mpp[i].size();j++){
mpp1[mpp[i][j]]++;
}
ll mx = 0;
ll mx1 = 0;
for(auto x:mp1){
// cout<<x.f<<" "<<x.s<<endl;
mx = max(mx,x.s);
}
for(auto x:mpp1){
// cout<<x.f<<" "<<x.s<<endl;
mx1 = max(mx1,x.s);
}
ans+=mx;
if(mx1==mx){
ans++;
}
}
cout<<ans<<endl;
}
int32_t main()
{
fastio
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}