import java.io.*; import java.util.*; public class Main_yukicoder460 { private static Scanner sc; private static Printer pr; private static void solve() { int m = sc.nextInt(); int n = sc.nextInt(); int[][] a = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i][j] = sc.nextInt(); } } Map hm = new HashMap<>(); Map hm2 = new HashMap<>(); { int[] mask = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (Math.abs(i - j) <= 1) { mask[i] |= 0x1 << (n - 1 - j); } } } for (int i = 0; i < 0x1 << n; i++) { long ans = 0; for (int j = 0; j < n; j++) { if ((i & 0x1 << j) != 0) { ans ^= mask[j]; } } if (hm.containsKey(ans)) { if (hm.get(ans) > Integer.bitCount(i)) { hm.put(ans, Integer.bitCount(i)); hm2.put(ans, i); } } else { hm.put(ans, Integer.bitCount(i)); hm2.put(ans, i); } } } long[][] conv = new long[m][n]; int[][] tmp = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int ii = 0; ii < m; ii++) { for (int jj = 0; jj < n; jj++) { if (Math.abs(ii - i) <= 1 && Math.abs(jj - j) <= 1) { tmp[ii][jj] = 1; } else { tmp[ii][jj] = 0; } } } conv[i][j] = enc(tmp); } } long st = enc(a); int min = Integer.MAX_VALUE; for (int i = 0; i < 0x1 << n; i++) { long ans = st; for (int j = 0; j < n; j++) { if ((i & 0x1 << j) != 0) { ans ^= conv[0][j]; } } int cnt = Integer.bitCount(i); for (int j = 1; j < m; j++) { long line = getLine(ans, j - 1, m, n); cnt += hm.get(line); int mask = hm2.get(line); for (int k = 0; k < n; k++) { if ((mask & 0x1 << k) != 0) { ans ^= conv[j][k]; } } } if (getLine(ans, m - 1, m, n) == 0) { min = Math.min(min, cnt); } } if (min == Integer.MAX_VALUE) { pr.println("Impossible"); } else { pr.println(min); } } private static long enc(int[][] a) { int m = a.length; int n = a[0].length; long tmp = 0; for (int i = 0; i < m; i++) { tmp <<= 8; for (int j = 0; j < n; j++) { if (a[i][j] == 1) { tmp |= 0x1 << n - 1 - j; } } } return tmp; } // k行目を取り出す private static long getLine(long a, int k, int m, int n) { return (a >> 8 * (m - 1 - k)) & 0xff; } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }