public static void main ( String [ ] args ) throws IOException {
  Scanner in = new Scanner ( System . in ) ;
  int N = in . nextInt ( ) ;
  int C = in . nextInt ( ) ;
  int [ ] [ ] D = new int [ C ] [ C ] ;
  long [ ] [ ] d = new long [ 3 ] [ C ] ;
  for ( int i = 0 ;
  i < C ;
  i ++ ) {
    for ( int j = 0 ;
    j < C ;
    j ++ ) D [ i ] [ j ] = in . nextInt ( ) ;
  }
  int [ ] [ ] c = new int [ N ] [ N ] ;
  for ( int i = 0 ;
  i < N ;
  i ++ ) {
    for ( int j = 0 ;
    j < N ;
    j ++ ) {
      c [ i ] [ j ] = in . nextInt ( ) ;
      for ( int k = 1 ;
      k <= C ;
      k ++ ) {
        d [ ( ( i + j ) % 3 ) ] [ k - 1 ] += D [ c [ i ] [ j ] - 1 ] [ k - 1 ] ;
      }
    }
  }
  long min = Long . MAX_VALUE ;
  for ( int i = 1 ;
  i <= C ;
  i ++ ) {
    for ( int j = 1 ;
    j <= C ;
    j ++ ) {
      if ( i == j ) continue ;
      for ( int k = 1 ;
      k <= C ;
      k ++ ) {
        if ( i == k || j == k ) continue ;
        long cost = 0 ;
        cost += d [ 0 ] [ i - 1 ] ;
        cost += d [ 1 ] [ j - 1 ] ;
        cost += d [ 2 ] [ k - 1 ] ;
        min = Math . min ( min , cost ) ;
      }
    }
  }
  System . out . println ( min ) ;
}

public static void main ( String [ ] args ) throws IOException { Scanner in = new Scanner ( System . in ) ; int N = in . nextInt ( ) ; int C = in . nextInt ( ) ; int [ ] [ ] D = new int [ C ] [ C ] ; long [ ] [ ] d = new long [ 3 ] [ C ] ; for ( int i = 0 ; i < C ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) D [ i ] [ j ] = in . nextInt ( ) ; } int [ ] [ ] c = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { c [ i ] [ j ] = in . nextInt ( ) ; for ( int k = 1 ; k <= C ; k ++ ) { d [ ( ( i + j ) % 3 ) ] [ k - 1 ] += D [ c [ i ] [ j ] - 1 ] [ k - 1 ] ; } } } long min = Long . MAX_VALUE ; for ( int i = 1 ; i <= C ; i ++ ) { for ( int j = 1 ; j <= C ; j ++ ) { if ( i == j ) continue ; for ( int k = 1 ; k <= C ; k ++ ) { if ( i == k || j == k ) continue ; long cost = 0 ; cost += d [ 0 ] [ i - 1 ] ; cost += d [ 1 ] [ j - 1 ] ; cost += d [ 2 ] [ k - 1 ] ; min = Math . min ( min , cost ) ; } } } System . out . println ( min ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) (throws (type_identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (decimal_integer_literal)) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (parenthesized_expression (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (decimal_integer_literal)))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (continue_statement)) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (continue_statement)) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))