Language: C++

#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define fr(i,k,N) for(ll i=k; i<N;i++)
#define rep(i,N) for(ll i=0; i<N;i++)
#define alll(v) v.begin(),v.end()
using namespace std;
template <class X> void pr(X test){cout<<test<<endl;}
template <class X> void prr(X test){for(auto it:test){cout<<it;}cout<<endl;}


int main() {
    ll n,c;
    cin>>n>>c;
    ll d[c+1][c+1];
    ll m[n+1][n+1];
    rep(i, c) rep(j, c){
        cin >> d[i+1][j+1];
    }
    rep(i, n) rep(j, n){
        cin >> m[i+1][j+1];
    }
    vector<pair<int, int>> v[3];
    rep(i, n) rep(j, n){
        auto tt = (i+1+j+1)%3;
        v[tt].push_back(make_pair(i+1, j+1));
    }
    ll sum = LONG_MAX;
    fr(i, 1, c+1) fr(j, i+1, c+1) fr(k, j+1, c+1){
        // v[0] => i
        // v[1] => j
        // v[2] => k
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ i];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ j];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ k];
            sum = min(sum,tmpsum);
        }
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ i];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ k];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ j];
            sum = min(sum,tmpsum);
        }
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ j];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ i];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ k];
            sum = min(sum,tmpsum);
        }
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ j];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ k];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ i];
            sum = min(sum,tmpsum);
        }
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ k];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ j];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ i];
            sum = min(sum,tmpsum);
        }
        {
            ll tmpsum = 0;
            for(auto it:v[0]) tmpsum+=d[ m[it.first][it.second] ][ k];
            for(auto it:v[1]) tmpsum+=d[ m[it.first][it.second] ][ i];
            for(auto it:v[2]) tmpsum+=d[ m[it.first][it.second] ][ j];
            sum = min(sum,tmpsum);
        }
    }
    pr(sum);
}



Compiler output:
a.cc: In function 'int main()':
a.cc:43:14: error: 'LONG_MAX' was not declared in this scope
   43 |     ll sum = LONG_MAX;
      |              ^~~~~~~~
a.cc:18:1: note: 'LONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   17 | #include <unordered_set>
  +++ |+#include <climits>
   18 | #define ll long long