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(); } } long st = enc(a); 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 * n + j] = enc(tmp); } } int[][] ans = new int[m][n]; long goal = enc(ans); Set hs = new HashSet<>(); hs.add(goal); for (int i = 0; i < m * n * 2; i++) { if (hs.contains(st)) { pr.println(i); return; } Set hstmp = new HashSet<>(); for (long e : hs) { for (long c : conv) { hstmp.add(e ^ c); } } hs = hstmp; } pr.println("Impossible"); } 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++) { for (int j = 0; j < n; j++) { tmp <<= 1; if (a[i][j] == 1) { tmp |= 0x1; } } } return tmp; } // --------------------------------------------------- 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); } } }