import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.HashMap; import java.util.stream.Collectors; public class No1345 { public static class Combination { public static List> make (List candidate, int r) { if (candidate.size() < r || candidate.size() <= 0 || r <= 0) { List> empty = new ArrayList<>(); empty.add(new ArrayList<>()); return empty; } List> combination = new ArrayList<>(); 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 List diag; public int cnt; public int[] col; public int size; public Row(int n) { row = new ArrayList(); diag = new ArrayList(); cnt = 0; col = new int[n]; size = 0; } public Row(Row r) { row = r.row; diag = new ArrayList(); cnt = r.cnt; col = new int[r.col.length]; size = r.size; } public void SetCol(int[][] A) { for (int i=0; i < col.length; i++) { for (int j : row) { if (!ContainDiag(j, i)) { col[i] += A[j][i]; } } } } public void SetDiag(int row, int col) { int i = (row << 16) + col; if (!diag.contains(i)) { diag.add(i); } } public boolean ContainDiag(int row, int col) { int i = (row << 16) + col; return diag.contains(i); } } 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<<(N+2)]; for (int i=0; i < 1 << (N+2); i++) { Row row = null; if ((i & (3 << N)) == 0) { row = new Row(N); for (int j=0; j < N; j++) { if ((i & (1 << j)) != 0) { row.cnt += RowSum[j]; row.size++; } else { row.row.add(j); } } } else { row = new Row(RowBlt[i & ((1 << N) - 1)]); for (int j = 0; j < N; j++) { if (row.row.contains(j)) { if ((i & (1 << N)) != 0) { row.cnt += A[j][j]; row.SetDiag(j, j); } if ((i & (1 << (N+1))) != 0) { if (!row.ContainDiag(j, N-1-j)) { row.cnt += A[j][N-1-j]; row.SetDiag(j, N-1-j); } } } } if ((i & (1 << N)) != 0) { row.size++; } if ((i & (1 << (N+1))) != 0) { row.size++; } } row.SetCol(A); RowBlt[i] = row; } List Range = new ArrayList(); for (int i=0; i < N; i++) { Range.add(i); } int ans = Integer.MAX_VALUE; HashMap>> combDict = new HashMap>>(); for (Row row : RowBlt) { if (row.size > M || M - row.size > N) { continue; } else if (row.size == M) { ans = Math.min(ans, row.cnt); } else { if (row.cnt >= ans) { continue; } List> combination = null; if (combDict.containsKey(M-row.size)) { combination = combDict.get(M-row.size); } else { combination = Combination.make(Range, M-row.size); combDict.put(M-row.size, combination); } for (List comb : combination) { int n = 0; for (int c : comb) { n += row.col[c]; if (row.cnt + n >= ans) { break; } } ans = Math.min(ans, row.cnt + n); } } } System.out.println(ans); } }