import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class No1345 { public static class Combination { // 候補となるリストと、何個ピックアップするかを渡す public static List> make (List candidate, int r) { // 5C6みたいなのは空 // 0C5も空 // 5C0も空 if (candidate.size() < r || candidate.size() <= 0 || r <= 0) { List> empty = new ArrayList<>(); empty.add(new ArrayList<>()); return empty; } List> combination = new ArrayList<>(); // 5C3だったら、添字0, 1, 2だけ考えたらいい for (int i = 0; i <= candidate.size() - r; i++) { // 一つ取り出して Integer picked = candidate.get(i); List rest = new ArrayList<>(candidate); // 以降の文字を削って rest.subList(0, i + 1).clear(); // 再帰呼び出しし、得られたリストの全ての先頭に取り出したものを結合する combination.addAll(make(rest, r - 1).stream().map(list -> { list.add(0, picked); return list; }).collect(Collectors.toList())); } return combination; } } private static class Row { public List row; public int cnt; public Row() { row = new ArrayList(); cnt = 0; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); int[] RowSum = new int[N]; int[][] A = new int[N][N]; for (int i=0; i < N; i++) { int sum = 0; for (int j=0; j < N; j++) { int a = scan.nextInt(); sum += a; A[i][j] = a; } RowSum[i] = sum; } scan.close(); Row[] RowBlt = new Row[1< Range = new ArrayList(); for (int i=0; i < N+2; i++) { Range.add(i); } int ans = Integer.MAX_VALUE; for (Row row : RowBlt) { if (row.row.size() > M || M - row.row.size() > N+2) { continue; } else if (row.row.size() == M) { ans = Math.min(ans, row.cnt); } else { for (List comb : Combination.make(Range, M-row.row.size())) { int n = 0; for (int c : comb) { if (c == N) { for (int i=0; i < N; i++) { if (!row.row.contains(i) && !comb.contains(i)) { n += A[i][i]; } } } else if (c == N+1) { for (int i=0; i < N; i++) { if (!row.row.contains(i) && !comb.contains(N-1-i) && !(comb.contains(N) && i == N-1-i)) { n += A[i][N-1-i]; } } } else { for (int i=0; i < N; i++) { if (!row.row.contains(i)) { n += A[i][c]; } } } } ans = Math.min(ans, row.cnt + n); } } } System.out.println(ans); } }