#define NOMINMAX
#define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= (int)b; --i)
#define range(i, a, b, c) for (int i = a;             \
                                   c > 0 ? i < b : i > b; \
                                   i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << " "
#define dumpl(x) dump(x) << endl
#else
const bool test = false;
#define dump(x) 
#define dumpl(x)
#endif

#else
const bool test = false;
#define dump(x) 
#define dumpl(x)
#endif

int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep)
{
	vector<string> v;
	stringstream ss(str);
	string buffer;
	while (getline(ss, buffer, sep))
	{
		v.push_back(buffer);
	}
	return v;
}

string join(const vector<string>& v, const char* delim = 0) {
	string s;
	if (!v.empty()) {
		s += v[0];
		for (decltype(v.size()) i = 1, c = v.size(); i < c; ++i) {
			if (delim) s += delim;
			s += v[i];
		}
	}
	return s;
}

template <class InputIterator>
int sum(InputIterator begin, InputIterator end)
{
	return accumulate(begin, end, 0ll);
}

template<typename T>
T gcd(T a, T b)
{
	T c;
	while (a != 0) {
		c = a; a = b%a;  b = c;
	}
	return b;
}

template<typename T>
T bit_count(T N) {
	T cnt = 0;
	while (N) {
		if (N & 1) cnt++;
		N >>= 1;
	}
	return cnt;
}

template<typename T>
void print_vec(ostream& ostream, vector<T> a) {
	rep(i, a.size() - 1) ostream << a[i] << " ";
	ostream << a.back() << endl;
}

template<class T>
T pow_n(T x, T n) {
	T res = 1;
	while (n > 0) {
		if (n & 1) res = res * x;
		x = x * x;
		n >>= 1;
	}
	return res;
}

class Solver {
public:
	int H, W;

	bool grid_ng(int y, int x) {
		return y < 0 || y >= H || x < 0 || x >= W;
	}

	struct Point {
		int y, x;
		bool operator < (const Point& r) const {
			if (y != r.y) return y < r.y;
			return x < r.x;
		}
	};

	struct Color {
		int c, cnt;
		bool operator < (const Color& r) const {
			return cnt < r.cnt;
		}
		void print() {
			cerr << "color : " << c + 1 << " " << cnt << endl;
		}
	};
	
	int N, C;
	int D[35][35];
	int c[505][505];

	void solve() {
		cin >> N >> C;
		rep(y, C) rep(x, C) cin >> D[y][x];
		rep(y, N) rep(x, N) {
			cin >> c[y][x];
			c[y][x] -= 1;
		}
		// int cnt[3][3][35] = {};
		vector<Color> cnt[3];
		vector<Color> cost[3];
		rep(i, 3) {
			cnt[i].resize(C);
			rep(c, C) cnt[i][c] = { c, 0 };
		}
		rep(y, N) rep(x, N) {
			int moded_n = (y + x) % 3;
			if (c[y][x] < 0) continue;
			cnt[moded_n][c[y][x]].cnt += 1;
		}
		rep(i, 3) {
			for (auto& c : cnt[i]) {
				c.print();
			}
			cerr << endl;
		}
		rep(i, 3) {
			cost[i].resize(C);
			rep(c, C) cost[i][c].cnt = 0;
			rep(c, C) cost[i][c].c = c;
			rep(c, C) rep(c2, C){
				cost[i][c2].cnt += D[c][c2] * cnt[i][c].cnt;
			}
		}
		rep(i, 3) {
			for (auto& c : cost[i]) {
				c.print();
			}
			cerr << endl;
		}
		int res = INF;
		rep(a, C) rep(b, C) rep(c, C) {
			if (a == b) continue;
			if (b == c) continue;
			if (c == a) continue;
			int d = cost[0][a].cnt + cost[1][b].cnt + cost[2][c].cnt;
			dump(a); dump(b); dump(c); dumpl(d);
			chmin(res, d);
		}
		cout << res << endl;
	}

};

signed main()
{
	srand((unsigned int)time(NULL));
	cout << fixed << setprecision(20);
	auto ptr = new Solver();
	ptr->solve();
	delete ptr;
	//while (true) {
	//	if (cin.eof() == true) break;
	//	auto ptr = new Solver();
	//	ptr->solve();
	//	delete ptr;
	//}
	return 0;
}


Measured memory_bytes: 2304