#include <iostream>
#include <climits>
#include <cassert>

using namespace std;

const int MAX_SIZE = 100*1000;

int cur_row[MAX_SIZE + 10][6] = {0};
int prev_row[MAX_SIZE + 10][6] = {0};

int row[MAX_SIZE + 10] = {0};

int main()
{
	freopen("dice.in", "rt", stdin);
	//freopen("dice.out", "wt", stdout);

	int testn;
	cin >> testn;

	for (int testi = 0; testi < testn; testi++)
	{
		int n, m;
		cin >> n >> m;
		assert(n >= 1 && n <= MAX_SIZE);
		assert(m >= 1 && m <= MAX_SIZE);
		assert(1ll*n*m <= 1ll*MAX_SIZE);

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cin >> row[j];
				assert(row[j] >= -1000 && row[j] <= 1000);
			}
			
			for (int j = 0; j < m; j++)
			{
				for (int k = 0; k < 6; k++)
				{
					prev_row[j][k] = cur_row[j][k];
					cur_row[j][k] = 0;
				}
			}

			for (int j = 0; j < m; j++)
			{
				for (int k = 0; k < 6; k++)
				{
					int maximum = INT_MIN;
					if (i > 0)
					{
						for (int k1 = 0; k1 < 6; k1++)
						{
							if (k1 == k || (k + k1) == 5)
								continue;
							maximum = max(maximum, prev_row[j][k1]);
						}
					}
					if (j > 0)
					{
						for (int k2 = 0; k2 < 6; k2++)
						{
							if (k2 == k || (k + k2) == 5)
								continue;
							maximum = max(maximum, cur_row[j - 1][k2]);
						}
					}

					cur_row[j][k] = row[j]*(k + 1);
					if (maximum != INT_MIN)
						cur_row[j][k] += maximum;
				}
			}
		}

		int maximum = INT_MIN;
		for (int k = 0; k < 6; k++)
		{
			maximum = max(maximum, cur_row[m - 1][k]);
		}
		cout << maximum << endl;
	}

	return 0;
}