#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define mt make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; int a[8][8]; int ans = INT_MAX; int M, N; bool check(int y, int x) { if(y > 0 && x > 0 && a[y-1][x-1])return false; if(y > 0 && x == N - 1 && a[y - 1][x])return false; if(y == M - 1 && x > 0 && a[y][x - 1])return false; if(y == M - 1 && x == N - 1 && a[y][x])return false; } void dfs(int y, int x, int cnt) { if(y == M) { rep(i, N)rep(j, M)if(a[i][j])return; smin(ans, cnt); return; } int ny = y, nx = x + 1; if(nx == N) { ny = y + 1; nx = 0; } dfs(ny, nx, cnt); for(int yy = max(y - 1, 0); yy < min(M, y + 2); ++yy) { for(int xx = max(x - 1, 0); xx < min(N, x + 2); ++xx) { a[yy][xx] = 1 - a[yy][xx]; } } dfs(ny, nx, cnt + 1); for(int yy = max(y - 1, 0); yy < min(M, y + 2); ++yy) { for(int xx = max(x - 1, 0); xx < min(N, x + 2); ++xx) { a[yy][xx] = 1 - a[yy][xx]; } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> M >> N; rep(i, M)rep(j, N) { cin >> a[i][j]; } dfs(0, 0, 0); if(ans == INT_MAX) { cout << "Impossible" << endl; } else { cout << ans << endl; } }