#!/usr/bin/env python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools INF = 1000 # %% M, N, *A = map(int, read().split()) # %% B = [0] * (N + 2) * (M + 2) for i in range(1, M + 1): B[(N + 2) * i + 1: (N + 2) * i + N + 1] = A[N * i - N: N * i] # %% def test(first_row, first_col): dxs = tuple((N + 2) * x + y for x in (-1, 0, 1) for y in (-1, 0, 1)) C = B[:] counter = 0 def push_button(i): nonlocal counter counter += 1 for dx in dxs: C[i + dx] ^= 1 for i, x in enumerate(first_row): if x: push_button(N + 3 + i) for j, x in enumerate(first_col, 2): if x: push_button((N + 2) * j + 1) for i in range(1, M): for j in range(1, N): if C[(N + 2) * i + j]: push_button((N + 2) * (i + 1) + (j + 1)) if any(C[M * (N + 2) + j] for j in range(1, N + 1)): return INF if any(C[i * (N + 2) + N] for i in range(1, M + 1)): return INF return counter # %% row_patterns = itertools.product([0, 1], repeat=N) col_patterns = itertools.product([0, 1], repeat=(M - 1)) answer = min(test(r, c) for r, c in itertools.product(row_patterns, col_patterns)) if answer == INF: answer = 'Impossible' print(answer)