using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NArr(n); var ans = int.MaxValue; var bitmax = 1 << n; for (var b = 0; b < bitmax; ++b) { for (var s = 0; s < 4; ++s) { var h = new int[n][]; for (var i = 0; i < n; ++i) h[i] = (int[]) map[i].Clone(); var sub = 0; var lines = 0; for (var j = 0; j < n; ++j) { if (((b >> j) & 1) != 0) { for (var i = 0; i < n; ++i) { sub += h[i][j]; h[i][j] = 0; } ++lines; } } if (s % 2 == 1) { for (var i = 0; i < n; ++i) if (((b >> i) & 1) == 0) { sub += h[i][i]; h[i][i] = 0; } ++lines; } if (s / 2 == 1) { for (var i = 0; i < n; ++i) if (((b >> i) & 1) == 0) { sub += h[n - i - 1][i]; h[n - i - 1][i] = 0; } ++lines; } var hsum = h.Select(hi => hi.Sum()).ToList(); hsum.Sort(); // WriteLine($"b={b}, s={s}, lines={lines}, hsum={string.Join(" ", hsum)}, sub={sub}, ans={sub + hsum.Take(Math.Min(n, Math.Max(0, m - lines))).Sum()}"); if (m - lines <= n) ans = Math.Min(ans, sub + hsum.Take(Math.Max(0, m - lines)).Sum()); } } WriteLine(ans); } }