import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { int M, N; readf("%s %s\n", &M, &N); auto A = new int[][M]; foreach (i; 0 .. M) A[i] = readln.chomp.split(" ").map!(to!int).array; auto X = new int[M]; foreach (i; 0 .. M) { foreach (j; 0 .. N) { if (A[i][j]) X[i] |= 1 << j; } } int flip(int bit) { int r = 0; for (int i = 0; i < N; i++) { if (! (bit & (1 << i))) continue; for (int dx = -1; dx <= 1; dx++) { int nx = i + dx; if (nx < 0 || nx >= N) continue; if (r & (1 << nx)) r &= ~(1 << nx); else r |= 1 << nx; } } return r; } int[Tuple!(int, int, int)] memo; int f(int y, int up, int cur) { // いまy行目, y-1行目の状態がup, y行目の状態がcur. auto key = tuple(y, up, cur); if (key in memo) return memo[key]; int ans = int.max / 4; if (y == M - 1) { for (int t = 0; t < (1 << N); t++) { auto s = flip(t); if ( (up ^ s) || (cur ^ s) ) continue; ans = min(ans, popcnt(t)); } } else { for (int t = 0; t < (1 << N); t++) { auto s = flip(t); if (up ^ s) continue; ans = min(ans, popcnt(t) + f(y + 1, cur ^ s, X[y + 1] ^ s)); } } return memo[key] = ans; } auto ans = int.max / 4; if (M == 1) { for (int t = 0; t < (1 << N); t++) { if (X[0] ^ flip(t)) continue; ans = min(ans, popcnt(t)); } } else { for (int t = 0; t < (1 << N); t++) { ans = min(ans, popcnt(t) + f(1, flip(t) ^ X[0], flip(t) ^ X[1])); } } if (ans >= int.max / 4) { writeln("Impossible"); } else { writeln(ans); } }