/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <limits.h>
using namespace std;
void conVert(long long *cost, char *spot)
{
if(*spot == 'R')
{
*cost += 5;
}
if(*spot == 'G')
{
*cost += 3;
}
}
int main() {
// your code goes here
int i,j,x,y,T,flag;
long long cost[4], min = LONG_MAX;
char cake[111][111];
char c1[111][111];
char c2[111][111];
cin >> T;
while(T--)
{
cost[0] = 0;
cost[1] = 0;
cost[2] = 0;
cost[3] = 0;
flag = 0;
min = LONG_MAX;
cin >> x >> y;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
cin >> cake[i][j];
if(flag == 0)
{
flag = 1;
c1[i][j] = 'R';
c2[i][j] = 'G';
}
else
{
flag = 0;
c1[i][j] = 'G';
c2[i][j] = 'R';
}
}
}
//Iterating horizontally(ROWS)
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
if(cake[i][j] != c1[i][j])
{
conVert(&cost[0], &cake[i][j]);
//cout <<"COST 1 :"<< cost[0] << endl;
}
if(cake[i][j] != c2[i][j])
{
conVert(&cost[1], &cake[i][j]);
//cout <<"COST 2 :"<< cost[1] << endl;
}
//cout << c2[i][j] << " ";
}//cout << endl;
}
//Iterating vertically(COLUMNS)
for(i = 0; i < y; i++)
{
for(j = 0; j < x; j++)
{
if(cake[i][j] != c1[i][j])
{
conVert(&cost[2], &cake[i][j]);
//cout <<"COST 3 :"<< cost[2] << endl;
}
if(cake[i][j] != c2[i][j])
{
conVert(&cost[3], &cake[i][j]);
//cout <<"COST 4 :"<< cost[3] << endl;
}
//cout << c2[i][j] << " ";
}//cout << endl;
}
for(i = 0; i < 4; i++)
{
//cout << cost[i] << endl;
if(min > cost[i])
min = cost[i];
}
cout << min << endl;
}
return 0;
}