#include <iostream>                                                             
#include <vector>                                                               
#include <limits>                                                               
using namespace std;                                                            
                                                                                
vector<vector<int> > m; // map                                                  
vector<vector<int> > d; // cost D                                               
vector<vector<int> > v; // parity and costs                                     
                                                                                
int n, c;                                                                       
                                                                                
int csm(int par, int color) {                                                   
  if (v[par][color] != 0)                                                       
    return v[par][color];                                                       
  int sumd = 0;                                                                 
  for (int x = 0; x < n; x++) {                                                 
    for (int y = 0; y < n; y++) {                                               
      if ((x + y) % 3 == par) {                                                 
        sumd += d[m[x][y] - 1][color];                                          
      }                                                                         
    }                                                                           
  }                                                                             
  v[par][color] = sumd;                                                         
  return sumd;                                                                  
}                                                                               
                                                                                
int main(void) {                                                                
  vector<int> nullvec;                                                          
                                                                                
  cin >> n >> c;                                                                
  for (int i = 0; i < c; i++) {                                                 
    d.push_back(nullvec);                                                       
    for (int j = 0; j < c; j++) {                                               
      int tmp;                                                                  
      cin >> tmp;                                                               
      d[i].push_back(tmp);                                                      
    }                                                                           
  }                                                                             
                                                                                
  for (int i = 0; i < n; i++) {                                                 
    m.push_back(nullvec);                                                       
    for (int j = 0; j < n; j++) {                                               
      int tmp;                                                                  
      cin >> tmp;                                                               
      m[i].push_back(tmp);                                                      
    }                                                                           
  }                                                                             
                                                                                
  int i[3]; // each parity color                                                
  int md = numeric_limits<int>::max();                                          
  vector<int> nullvec2(c, 0);                                                   
  v.assign(3, nullvec2);

                                                                               
  for (i[0] = 0; i[0] < c; i[0]++) {                                            
    for (i[1] = 0; i[1] < c; i[1]++) {                                          
      if (i[1] == i[0])                                                         
        continue;                                                               
      for (i[2] = 0; i[2] < c; i[2]++) {                                        
        if (i[2] == i[0] || i[2] == i[1])                                       
          continue;                                                             
        int tmp = 0;                                                            
        for (int j = 0; j < 3; j++) {                                           
          tmp += csm(j, i[j]);                                                  
        }                                                                       
        md = min(md, tmp);                                                      
      }                                                                         
    }                                                                           
  }                                                                             
  cout << md;                                                                   
  return 0;                                                                     
}

Measured memory_bytes: 1280