#include <iostream>
#include <climits>
using namespace std;
int m, n, s = 0, target;
int a[10][10];
bool v[10][10];
int ans = INT_MAX;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int x, int y, int sum, int count) {
if (sum > target || count >= ans) return; // 剪枝
if (sum == target) {
ans = min(ans, count);
return;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny]) {
v[nx][ny] = true;
dfs(nx, ny, sum + a[nx][ny], count + 1);
v[nx][ny] = false; // 回溯
}
}
}
int main() {
cin >> m >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
v[i][j] = false;
s += a[i][j];
}
}
if (s% 2 != 0) { // 总和为奇数,无法分割
cout << 0 << endl;
return 0;
}
target = s / 2;
v[0][0] = true;
dfs(0, 0, a[0][0], 1);
if (ans == INT_MAX) {
cout << 0 << endl; // 无法分割
} else {
cout << ans << endl;
}
return 0;
}