#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
const string narek = "narek";
void solve(){
int ans = 0;
int n , m;
cin >> n >> m;
vector<string> s(n);
for(int i = 0;i < n;i ++){
cin >> s[i];
}
vector<int> dp(5 , -1e9) , ndp;
dp[0] = 0;
for(int i = 0; i < n;i ++){
ndp = dp;
for(int k = 0;k < 5;k ++){
if(dp[k] == -1e9) continue;
int nxt = k , score = 0;
for(int j = 0;j < m;j ++){
int idx = narek.find(s[i][j]);
if(idx == -1) continue;
if(nxt == idx){
nxt = (nxt + 1) % 5;
score++;
}
else score--;
}
ndp[nxt] = max(ndp[nxt] , dp[k] + score);
}
dp = ndp;
}
for(int i = 0;i < 5;i ++) ans = max(ans , dp[i] - 2 * i);
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgdDsKCmNvbnN0IHN0cmluZyBuYXJlayA9ICJuYXJlayI7Cgp2b2lkIHNvbHZlKCl7CiAgICBpbnQgYW5zID0gMDsKICAgIGludCBuICwgbTsKICAgIGNpbiA+PiBuID4+IG07CiAgICB2ZWN0b3I8c3RyaW5nPiBzKG4pOwogICAgZm9yKGludCBpID0gMDtpIDwgbjtpICsrKXsKICAgICAgICBjaW4gPj4gc1tpXTsKICAgIH0KICAgIHZlY3RvcjxpbnQ+IGRwKDUgLCAtMWU5KSAsIG5kcDsKICAgIGRwWzBdID0gMDsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuO2kgKyspewogICAgICAgIG5kcCA9IGRwOwogICAgICAgIGZvcihpbnQgayA9IDA7ayA8IDU7ayArKyl7CiAgICAgICAgICAgIGlmKGRwW2tdID09IC0xZTkpIGNvbnRpbnVlOwogICAgICAgICAgICBpbnQgbnh0ID0gayAsIHNjb3JlID0gMDsKICAgICAgICAgICAgZm9yKGludCBqID0gMDtqIDwgbTtqICsrKXsKICAgICAgICAgICAgICAgIGludCBpZHggPSBuYXJlay5maW5kKHNbaV1bal0pOwogICAgICAgICAgICAgICAgaWYoaWR4ID09IC0xKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGlmKG54dCA9PSBpZHgpewogICAgICAgICAgICAgICAgICAgIG54dCA9IChueHQgKyAxKSAlIDU7CiAgICAgICAgICAgICAgICAgICAgc2NvcmUrKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2Ugc2NvcmUtLTsKICAgICAgICAgICAgfQogICAgICAgICAgICBuZHBbbnh0XSA9IG1heChuZHBbbnh0XSAsIGRwW2tdICsgc2NvcmUpOwogICAgICAgIH0KICAgICAgICBkcCA9IG5kcDsKICAgIH0KICAgIGZvcihpbnQgaSA9IDA7aSA8IDU7aSArKykgYW5zID0gbWF4KGFucyAsIGRwW2ldIC0gMiAqIGkpOwogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKfQoKc2lnbmVkIG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApO2NvdXQudGllKDApOwogICAgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==