#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define fi first
#define se second
#define REP(i, n) for (auto i = 0; i < (n); ++i)
#define FOR(i, a, b) for (auto i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (auto i = (a); i >= (b); --i)
#define FORE(i, a, b) for (auto i = (a); i < (b); ++i)
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define left ___left
#define right ___right
#define __builtin_popcount __builtin_popcountll
#define int long long
void you_make_it(void) {
map <char, int> dd;
dd['n'] = 0;
dd['a'] = 1;
dd['r'] = 2;
dd['e'] = 3;
dd['k'] = 4;
int n, m; cin >> n >> m;
vector <vector <char>> a(n, vector <char> (m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cin >> a[i][j];
}
vector <vector <int>> dp(n + 1, vector <int> (5, -1e9));
dp[0][0] = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int k = 0; k < 5; ++k) {
int r = k, res = 0;
for (int j = 0; j < m; ++j) {
if(not dd.count(a[i][j])) continue;
if(dd[a[i][j]] == r) res++, r = (r + 1) % 5;
else res--;
}
dp[i + 1][r] = max(dp[i + 1][r], dp[i][k] + res);
dp[i + 1][k] = max(dp[i + 1][k], dp[i][k]);
}
}
for (int i = 0; i < 5; ++i) ans = max(ans, dp[n][i] - 2 * i);
cout << ans;
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif // LOCAL
auto start_time = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
cin >> test;
for (int i = 0; i < test; ++i) {
you_make_it();
cout << '\n';
}
auto end_time = chrono::high_resolution_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 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biA+PiBtOwogICAgdmVjdG9yIDx2ZWN0b3IgPGNoYXI+PiBhKG4sIHZlY3RvciA8Y2hhcj4gKG0pKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyArK2opIGNpbiA+PiBhW2ldW2pdOwogICAgfQogICAgdmVjdG9yIDx2ZWN0b3IgPGludD4+IGRwKG4gKyAxLCB2ZWN0b3IgPGludD4gKDUsIC0xZTkpKTsKICAgIGRwWzBdWzBdID0gMDsKICAgIGludCBhbnMgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBmb3IgKGludCBrID0gMDsgayA8IDU7ICsraykgewogICAgICAgICAgICBpbnQgciA9IGssIHJlcyA9IDA7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgKytqKSB7CiAgICAgICAgICAgICAgICBpZihub3QgZGQuY291bnQoYVtpXVtqXSkpIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgaWYoZGRbYVtpXVtqXV0gPT0gcikgcmVzKyssIHIgPSAociArIDEpICUgNTsKICAgICAgICAgICAgICAgIGVsc2UgcmVzLS07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZHBbaSArIDFdW3JdID0gbWF4KGRwW2kgKyAxXVtyXSwgZHBbaV1ba10gKyByZXMpOwogICAgICAgICAgICBkcFtpICsgMV1ba10gPSBtYXgoZHBbaSArIDFdW2tdLCBkcFtpXVtrXSk7IAogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgNTsgKytpKSBhbnMgPSBtYXgoYW5zLCBkcFtuXVtpXSAtIDIgKiBpKTsKICAgIGNvdXQgPDwgYW5zOwp9CgpzaWduZWQgbWFpbigpIHsKCiNpZmRlZiBMT0NBTAogICAgZnJlb3BlbigiVEFTSy5pbnAiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIlRB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