#include <bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ldb;
const ll mod = 1e9 + 7;
ll K, N;
ll val[1811][1811];
ll DIAG1[1811][1811]; // NW SE
ll DIAG2[1811][1811]; // NE SW
ll WE[1811][1811];
ll NS[1811][1811];
ll CALC[1811][1811];
ll QUAD[4][1811][1811];
void rset(void)
{
memset(val, 0, sizeof(val));
memset(DIAG1, 0, sizeof(DIAG1));
memset(DIAG2, 0, sizeof(DIAG2));
memset(WE, 0, sizeof(WE));
memset(NS, 0, sizeof(NS));
memset(CALC, 0, sizeof(CALC));
memset(QUAD, 0, sizeof(QUAD));
}
ll NS_p(ll Y1, ll Y2, ll X)
{
Y1 = min(Y1, N + 2 * K);
Y2 = max(Y2, 0LL);
return NS[Y1][X] - NS[Y2][X];
}
ll WE_p(ll Y, ll X1, ll X2)
{
X1 = min(X1, N + 2 * K);
X2 = max(X2, 0LL);
return WE[Y][X1] - WE[Y][X2];
}
ll getdiag1(ll Y1, ll X1)
{
if(Y1 < 0 || X1 < 0) return 0;
else if(Y1 <= N+2*K && X1 <= N+2*K) return DIAG1[Y1][X1];
else
{
ll offset = max(Y1 - (N + 2 * K), X1 - (N + 2 * K));
Y1 -= offset; X1 -= offset;
if(Y1 < 0 || X1 < 0) return 0;
else return DIAG1[Y1][X1];
}
}
ll DIAG1_p(ll Y1, ll X1, ll Y2, ll X2)
{
return getdiag1(Y1, X1) - getdiag1(Y2, X2);
}
ll getdiag2(ll Y1, ll X1)
{
if(Y1 < 0 || X1 > N + 2 * K) return 0;
else if(Y1 <= N+2*K && X1 >= 0) return DIAG2[Y1][X1];
else
{
ll offset = max(Y1 - (N + 2 * K), -X1);
Y1 -= offset; X1 += offset;
if(Y1 < 0 || X1 < 0 || X1 > N + 2 * K) return 0;
else return DIAG2[Y1][X1];
}
}
ll DIAG2_p(ll Y1, ll X1, ll Y2, ll X2)
{
return getdiag2(Y1, X1) - getdiag2(Y2, X2);
}
ll getval(ll Y, ll X)
{
if(X<=0 || X>N+2*K || Y<=0 || Y>N+2*K) return 0;
return val[Y][X];
}
void finish_QUAD0(void)
{
ll i, j;
for(i=0 ; i<=K-1 && i<=1 ; i++)
for(j=0 ; i+j<=K-1 ; j++)
QUAD[0][1][1] += getval(1-i, 1+j);
for(i=1 ; i<=N+2*K ; i++)
{
for(j=1 ; j<=N+2*K ; j++)
{
if(i == 1 && j == 1) continue;
if(j == 1) // get from top
{
QUAD[0][i][j] = QUAD[0][i-1][j]
+ WE_p(i, j+K-1, j-1)
- DIAG1_p(i-1, j+K-1, i-K-1, j-1);
}
else // get from left
{
QUAD[0][i][j] = QUAD[0][i][j-1]
+ DIAG1_p(i, j+K-1, i-K, j-1)
- NS_p(i, i-K, j-1);
}
}
}
}
void finish_QUAD1(void)
{
ll i, j;
for(i=0 ; i<=K-1 && i<=1 ; i++)
for(j=0 ; i+j<=K-1 && j<=1; j++)
QUAD[1][1][1] += getval(1-i, 1-j);
for(i=1 ; i<=N+2*K ; i++)
{
for(j=1 ; j<=N+2*K ; j++)
{
if(i == 1 && j == 1) continue;
if(j == 1) // get from top
{
QUAD[1][i][j] = QUAD[1][i-1][j]
+ WE_p(i, j, j-K)
- DIAG2_p(i-1, j-K+1, i-K-1, j+1);
}
else // get from left
{
QUAD[1][i][j] = QUAD[1][i][j-1]
+ NS_p(i, i-K, j)
- DIAG2_p(i, j-K, i-K, j);
}
}
}
}
void finish_QUAD2(void)
{
ll i, j;
for(i=0 ; i<=K-1 ; i++)
for(j=0 ; i+j<=K-1 && j<=1 ; j++)
QUAD[2][1][1] += getval(1+i, 1-j);
for(i=1 ; i<=N+2*K ; i++)
{
for(j=1 ; j<=N+2*K ; j++)
{
if(i == 1 && j == 1) continue;
if(j == 1) // get from top
{
QUAD[2][i][j] = QUAD[2][i-1][j]
+ DIAG1_p(i+K-1, j, i-1, j-K)
- WE_p(i-1, j, j-K);
}
else // get from left
{
QUAD[2][i][j] = QUAD[2][i][j-1]
+ NS_p(i+K-1, i-1, j)
- DIAG1_p(i+K-1, j-1, i-1, j-K-1);
}
}
}
}
void finish_QUAD3(void)
{
ll i, j;
for(i=0 ; i<=K-1 ; i++)
for(j=0 ; i+j<=K-1 ; j++)
QUAD[3][1][1] += val[1+i][1+j];
for(i=1 ; i<=N+2*K ; i++)
{
for(j=1 ; j<=N+2*K ; j++)
{
if(i == 1 && j == 1) continue;
if(j == 1) // get from top
{
QUAD[3][i][j] = QUAD[3][i-1][j]
+ DIAG2_p(i+K-1, j, i-1, j+K)
- WE_p(i-1, j+K-1, j-1);
}
else // get from left
{
QUAD[3][i][j] = QUAD[3][i][j-1]
+ DIAG2_p(i+K-1, j, i-1, j+K)
- NS_p(i+K-1, i-1, j-1);
}
}
}
}
void solve(void)
{
ll i, j; rset(); cin >> N >> K;
for(i=1 ; i<=N ; i++)
for(j=1 ; j<=N ; j++)
cin >> val[K+i][K+j];
for(i=0 ; i<=N+2*K ; i++)
{
for(j=0 ; j<=N+2*K ; j++)
{
if(i!=0 && j!=0) DIAG1[i][j] = DIAG1[i-1][j-1] + val[i][j];
if(i!=0) DIAG2[i][j] = DIAG2[i-1][j+1] + val[i][j];
if(j!=0) WE[i][j] = WE[i][j-1] + val[i][j];
if(i!=0) NS[i][j] = NS[i-1][j] + val[i][j];
}
}
finish_QUAD0(); finish_QUAD1(); finish_QUAD2(); finish_QUAD3();
for(i=1 ; i<=K ; i++)
{
for(j=1 ; j<=K ; j++)
{
ll dist = abs(i-1) + abs(j-1);
if(dist <= K-1) CALC[1][1] += (K - dist) * val[i][j];
}
}
for(i=1 ; i<=N+2*K ; i++)
{
for(j=1 ; j<=N+2*K ; j++)
{
if(i == 1 && j == 1) continue;
if(j == 1) // get from top
{
CALC[i][j] = CALC[i-1][j];
CALC[i][j] += (QUAD[2][i][j] + QUAD[3][i][j] - NS_p(i+K-1, i-1, j));
CALC[i][j] -= (QUAD[0][i-1][j] + QUAD[1][i-1][j] - NS_p(i-1, i-K-1, j));
}
else // get from left
{
CALC[i][j] = CALC[i][j-1];
CALC[i][j] += (QUAD[0][i][j] + QUAD[3][i][j] - WE_p(i, j+K-1, j-1));
CALC[i][j] -= (QUAD[1][i][j-1] + QUAD[2][i][j-1] - WE_p(i, j-1, j-K-1));
}
}
}
ll ans = 0;
for(i=1 ; i<=N+2*K ; i++)
for(j=1 ; j<=N+2*K ; j++)
ans = max(ans, CALC[i][j]);
cout << ans << endl;
}
int main(void)
{
fio; ll i, tc; cin >> tc;
for(i=1 ; i<=tc ; i++)
{
cout << "Case #" << i << endl;
solve(); // endl in solve
}
return 0;
}