#include using namespace std; typedef long long ll; int H, W; ll f(ll k, int x, int y) { ll ret = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { int c = i * W + j; ret |= k & (1LL << c); if(x - 1 <= j && j <= x + 1 && y - 1 <= i && i <= y + 1) { ret ^= (1LL << c); } } } return ret; } void view(ll s) { for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { cout << (s >> (y * W + x) & 1) << " "; } cout << endl; } cout << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> H >> W; ll s = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { ll a; cin >> a; s |= a << (i * W + j); } } if(s == 0) { cout << 0 << endl; return 0; } unordered_map m; m[0] = 0; queue q; q.push(0); while(q.size()) { ll cur = q.front(); q.pop(); //view(cur); int t = m[cur]; if(cur == s) { cout << t << endl; return 0; } for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { ll nxt = f(cur, x, y); //view(nxt); if(m.count(nxt) == 0) { m[nxt] = t + 1; q.push(nxt); } } } } if(m.count(0)) { cout << m[0] << endl; } else { cout << "Impossible" << endl; } }