lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end n, m = read_line.split.map(&.to_i) a = (1..n).map { read_line.split.map(&.to_i) } puts [false, true].repeated_permutations(n + 2).min_of { |flag| cnt = flag.count(true) if cnt + n < m 10**9 else b = [0] * n sum = 0 (0...n).each { |i| (0...n).each { |j| if flag[i] || (flag[-2] && i == j) || (flag[-1] && i == (n - 1 - j)) sum += a[i][j] else b[j] += a[i][j] end } } if cnt >= m sum else sum + b.sort[0, m - cnt].sum end end }