#include<stdio.h> #include<string.h> int n , k , ans; const int maxn = 105; int map[maxn][maxn]; int dp[maxn][maxn]; int dir[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1}; int f(int x , int y){ int tx , ty; int ret = 0; if(dp[x][y] != 0) return dp[x][y]; for(int i = 1 ; i <= k ; i ++){ for(int j = 0 ; j < 4 ; j ++){ tx = x + dir[j][0] * i; ty = y + dir[j][1] * i; if(tx >= 0 && tx < n && ty >= 0 && ty < n && map[tx][ty] > map[x][y]){ int t = f(tx , ty); if(t > ret) ret = t; } } } //if(ret == -0x3f3f3f3f) ret = 0; dp[x][y] = ret + map[x][y]; if(ans < dp[x][y]) ans = dp[x][y]; return dp[x][y]; } int main(){ while(~scanf("%d%d" , &n , &k) && (n != -1 || k != -1)){ ans = 0; memset(dp , 0 , sizeof(dp)); for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ; j ++){ scanf("%d" , &map[i][j]); } } for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ; j ++){ f(i , j); } } printf("%d\n" , dp[0][0]); } return 0; }