#include <iostream> #include <cstring>#include <cstdio>#include <algorithm>#include <string>#include <string.h>#include <cmath>#include <sstream>#include <set>#include <map>#include <functional>#include <queue>#include <vector>usingnamespacestd;
constint maxn = 110;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int dp[maxn][maxn], chess[maxn][maxn];
int n, k;
int dfs(int x, int y)
{
int ans = 0;
if (!dp[x][y])
{
for (int i = 1; i <= k; i++)
{
for (int j = 0; j < 4; j++)
{
int nowx = x + dx[j] * i;
int nowy = y + dy[j] * i;
if (nowx >= n || nowx < 0 || nowy >= n || nowy < 0)
continue;
if (chess[nowx][nowy] > chess[x][y])
ans = max(ans, dfs(nowx, nowy));
}
}
dp[x][y] = ans + chess[x][y];
}
return dp[x][y];
}
int main()
{
while (cin >> n >> k&&n != -1)
{
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> chess[i][j];
}
cout << dfs(0, 0) << endl;
}
}