#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <utility>
#include <cctype>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

#define C const
#define R register
#define fast ios::sync_with_stdio(0);cin.tie(0)
#define ll long long
#define ld long double
#define ull unsigned long long
#define eps 1e-8
#define pi 3.141592653589793
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define PII pair<int,int>
#define sqr(a) ((a)*(a))
#define ls rt<<1
#define rs rt<<1|1
#define lowbit(i) ((i)&(-(i)))
#define FOR(i,a,b) for(R int i=(a);i<=(b);++i)
#define REP(i,a,b) for(R int i=(a);i>=(b);--i)
#define FOR_edge(i)  for(R int i=head[u];i;i=e[i].nxt)

using namespace std;

const int dx[]={1,-1,0,0};
const int dy[]={0,0,-1,1};

void judge() {
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
}
inline char nc() {
	static char buf[1000001],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template <typename T> inline T Read() {
	R T s=0,w=1;
	R char c=nc();
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=nc();}
	while(c>='0'&&c<='9')s=(s<<1)+(s<<3)+c-48,c=nc();
	return s*w;
}
template <typename T> inline void Read(R T &s) {
	s=0;
	R T w=1;
	R char c=nc();
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=nc();}
	while(c>='0'&&c<='9')s=(s<<1)+(s<<3)+c-48,c=nc();
	s*=w;
}
template <typename T> inline void write(R T x) {
	if(x<0)  putchar('-'),x=-x;
	if(x>9)  write(x/10);
	putchar(x%10+'0');
}


template <typename T> inline T gcd(C T &x,C T &y){return(y==0?x:gcd(y,x%y));}
template <typename T> inline void Swap(R T &x,R T &y){x^=y;y^=x;x^=y;}
template <typename T> inline void Swap_com(R T &x,R T &y){C T t=x;x=y;y=t;}
template <typename T> inline T POW(T x,T p,T mod) {
	R T res=1;
	while(p) {
		if(p&1)  res=(res*x)%mod;
		x=(x*x)%mod;  p>>=1;
	}
	return res;
}

///////////////////////////////////////////////////////////////////////////////
/////////////////////////////       Program       /////////////////////////////
/////////////////////////////        Heads        /////////////////////////////
/////////////////////////////    Don not touch    /////////////////////////////
///////////////////////////////////////////////////////////////////////////////

#define read Read<int>
#define readll Read<ll>

int ans=(1<<30);
int n,cc,d[50][50];
int c[505][505];
bool vis[50];
int line[1002][30];
//int key[4][30];

inline void col(int dep,int sum) {
	if(sum>=ans)  return;
	if(dep==3) {
		ans=sum;
		return;
	}
	for(int p=1;p<=cc;++p) {
		if(vis[p])  continue;
		vis[p]=1;
		int res=0;
		if(dep==0) {
			for(int i=3;i<=2*n;i+=3)  res+=line[i][p];
		} else if(dep==1) {
			for(int i=4;i<=2*n;i+=3)  res+=line[i][p];
		} else  for(int i=2;i<=2*n;i+=3)  res+=line[i][p];
		col(dep+1,sum+res);
		vis[p]=0;
	}
}
int main() {
//	judge();
	cin>>n>>cc;
	FOR(i,1,cc)  FOR(j,1,cc)  cin>>d[i][j];
	FOR(i,1,n)  FOR(j,1,n)  cin>>c[i][j];
	FOR(co,1,cc) {
		FOR(i,1,n)
			FOR(j,1,n)
				if(c[i][j]!=co)  line[i+j][co]+=d[c[i][j]][co];
	}
	col(0,0);
	cout<<ans;
	return 0;
}

Measured memory_bytes: 1408