#include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i)) typedef long long ll; using namespace std; template void setmin(T & a, T const & b) { if (b < a) a = b; } template auto vectors(X x, T a) { return vector(x, a); } template auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector(x, cont); } ll bit(int i) { return 1ll << i; } bool is_on_field(int y, int x, int h, int w) { return 0 <= y and y < h and 0 <= x and x < w; } const int inf = 1e9+7; int main() { int h, w; cin >> h >> w; vector > a = vectors(h, w, bool()); repeat (y,h) repeat (x,w) { bool it; cin >> it; a[y][x] = it; } int ans = inf; repeat (s, bit(w)) { repeat (t, bit(h)) { int cnt = 0; vector > b = a; auto flip = [&](int y, int x) { cnt += 1; repeat_from (dy,-1,1+1) repeat_from (dx,-1,1+1) { int ny = y + dy; int nx = x + dx; if (is_on_field(ny, nx, h, w)) { b[ny][nx] = not b[ny][nx]; } } }; repeat (y,h) { repeat (x,w) { if (y == 0) { if (s & bit(x)) { flip(y, x); } } else if (x == 0) { if (t & bit(y-1)) { flip(y, x); } } else { if (b[y-1][x-1]) { flip(y, x); } } } } bool succeeded = true; repeat (y,h) { repeat (x,w) { if (b[y][x]) { succeeded = false; } } } if (succeeded) { setmin(ans, cnt); } } } if (ans == inf) ans = -1; cout << ans << endl; return 0; }