import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class Dice 
{
	public static void main(String[] args) throws Exception
	{
		new Dice().solve();
	}

	final int MAX_SIZE = 100*1000;
	
	StreamTokenizer in = null;
	//Scanner in = null;
	PrintWriter out = null;

	int nextInt() throws Exception
	{
		in.nextToken();
		return (int)in.nval;
		//return in.nextInt();
	}
	
	private void solve() throws Exception
	{
		in = new StreamTokenizer(new BufferedReader(new FileReader("dice.in")));
		//in = new Scanner(new FileReader("dice.in"));
		//out = new PrintWriter(new FileWriter("dice.out"));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		
		int row[] = new int[MAX_SIZE];
		int cur_row[][] = new int[MAX_SIZE][6];
		int prev_row[][] = new int[MAX_SIZE][6];
		int g[][] = {{1,2,3,4},{0,2,3,5},{0,1,4,5},{0,1,4,5},{0,2,3,5},{1,2,3,4}};
		
		int testn = nextInt();
		for (int testi = 0; testi < testn; testi++)
		{
			int n = nextInt();
			int m = nextInt();

			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < m; j++)
				{
					row[j] = nextInt();
				}
				
				int buf[][] = cur_row;
				cur_row = prev_row;
				prev_row = buf;
				
				for (int j = 0; j < m; j++)
				{
					for (int k = 0; k < 6; k++)
					{
						int maximum = Integer.MIN_VALUE;
						if (i > 0)
						{
							for (int k1 = 0; k1 < 4; k1++)
							{
								maximum = Math.max(maximum, prev_row[j][g[k][k1]]);
							}
						}
						if (j > 0)
						{
							for (int k2 = 0; k2 < 4; k2++)
							{
								maximum = Math.max(maximum, cur_row[j - 1][g[k][k2]]);
							}
						}

						cur_row[j][k] = row[j]*(k + 1);
						if (maximum != Integer.MIN_VALUE)
						{
							cur_row[j][k] += maximum;
						}
					}
				}
			}

			int maximum = Integer.MIN_VALUE;
			for (int k = 0; k < 6; k++)
			{
				maximum = Math.max(maximum, cur_row[m - 1][k]);
			}
			out.println(maximum);
		}
		
		out.close();
	}
}

