import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
static int[] dr = {1,0,-1,0};
static int[] dc = {0,1,0,-1};
static int[] lr = {1,1,-1,-1};
static int[] lc = {1,-1,1,-1};
static int[][] map;
static int sum=0;
static int max = 0;
static int N;
static int M;
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
N = sc.nextInt();
M = sc.nextInt();
map = new int[N][N];
max=0;
for(int i =0; i<N; i++){
for(int j = 0; j<N; j++){
map[i][j] = sc.nextInt();
}
}
for(int i =0; i<N; i++){
for(int j = 0; j<N; j++){
sum = map[i][j];
for(int k = 0; k<4; k++){
ddfs(i,j,k,1);// 가로,세로로 분사
}
max = Math.max(sum,max);
sum = map[i][j];
for(int k = 0; k<4; k++){
ldfs(i,j,k,1);// 대각선으로 분사
}
max = Math.max(sum,max);
}
}
System.out.println("#"+test_case+" "+max);
}
}
public static void ddfs(int row, int col,int dir,int count){
if(count==M) return;
int nrow = row + dr[dir];
int ncol = col + dc[dir];
if(nrow<0||ncol<0||nrow>=N||ncol>=N) return;
sum+=map[nrow][ncol];
ddfs(nrow,ncol,dir,count+1);
}
public static void ldfs(int row, int col,int dir,int count){
if(count==M) return;
int nrow = row + lr[dir];
int ncol = col + lc[dir];
if(nrow<0||ncol<0||nrow>=N||ncol>=N) return;
sum+=map[nrow][ncol];
ldfs(nrow,ncol,dir,count+1);
}
}
특이하게 대각선 방향과 가로,세로 방향의 값을 더해서 최댓값을 구하게 하는 문제였다.