import itertools import numpy as np N, M = map(int, input().split()) A = np.array([list(map(int, input().split())) for _ in range(N)]) def make_table(t, slash, backslash): fancy = np.nonzero(t) table = A.copy() solved = 0 solved += np.sum(table[fancy, :]) table[fancy, :] = 0 if slash: for i in range(N): solved += table[i, i] table[i, i] = 0 if backslash: for i in range(N): solved += table[N - i - 1, i] table[N - i - 1, i] = 0 return table, solved ans = 100 * N * N # 横と斜めは全列挙 for t in itertools.product((0, 1), repeat=N): row = t.count(1) if not M - N - 2 <= row <= M: continue for slash, backslash in ((0, 0), (1, 0), (0, 1), (1, 1)): col = M - row - slash - backslash # 削るべき列数 if not 0 <= col <= N: continue table, solved = make_table(t, slash, backslash) # 縦のうれしい順に選ぶ additional_solved = np.sum(table, axis=0) additional_solved.sort() solved += sum(additional_solved[:col]) ans = min(ans, solved) print(ans)