#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 50010
#define L 1000100
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
int pre[N],last[N],e[N],ca[N],co[N],len=0,re[N],inf,source,target,q[L],inq[N],dis[N];
int path[N],n,k,a[51][51],p1,p2,p3,ano[N];
void add(int x,int y,int CA,int CO)
{
  pre[++len]=last[x];last[x]=len;e[len]=y;ca[len]=CA;co[len]=CO;re[len]=len+1;ano[len]=x; 
  pre[++len]=last[y];last[y]=len;e[len]=x;ca[len]=0;co[len]=-CO;re[len]=len-1;ano[len]=y;
}
int Nxt(int x){if (x==L-1) return 1;return x+1;}
int spfa(int v0)
{
  memset(inq,0,sizeof(inq));
  memset(dis,20,sizeof(dis)); 
  int h=0,t=1;inf=dis[0];
  q[1]=v0;dis[v0]=0;inq[v0]=1;
  while (h!=t)
  {
    h=Nxt(h);int u=q[h];
    for(int p=last[u];p;p=pre[p])
    {
      int v=e[p],rm=ca[p],cost=co[p];
      if (rm>0&&dis[u]+cost<dis[v])
      {
        dis[v]=dis[u]+cost;
        path[v]=p;
        if (!inq[v])
        {
          t=Nxt(t);
          q[t]=v;
          inq[v]=1;
        }
      }
    }
    inq[u]=0;
  }
  if (dis[target]==inf) return 0;return 1;
}
int change()
{
  int i;
  int Min=inf,Cost=0;
  for(i=path[target];ano[i]!=source;i=path[ano[i]]) Min=min(Min,ca[i]);
  for(i=path[target];ano[i]!=source;i=path[ano[i]])
  {
    ca[i]-=Min;
    ca[re[i]]+=Min;
    Cost+=Min*co[i];
  }
  return Cost;
}
int MCMF()
{
  int __ans=0;
  while (spfa(source)) __ans+=change();
  return __ans;
}
int getpos(int x,int y){return (x-1)*n+y;}
int main()
{
  scanf("%d %d",&n,&k);int i,j;
  FOR(i,1,n) FOR(j,1,n) scanf("%d",&a[i][j]);
  FOR(i,1,n)
   FOR(j,1,n)
   {
     int p=getpos(i,j);
     int p1=p*2,p2=p*2+1;
     add(p1,p2,k-1,0);
     add(p1,p2,1,-a[i][j]);
     if (i<n) 
     {
       p3=getpos(i+1,j)*2;
       add(p2,p3,k,0);
     }
     if (j<n)
     {
       p3=getpos(i,j+1)*2;
       add(p2,p3,k,0);
     }
   }
  source=n*n*2+2;
  target=n*n*2+3;
  add(source,1*1*2,k,0);
  add(n*n*2+1,target,k,0);
  int Cost=MCMF();
  printf("%d\n",-Cost);
}