#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[s] = 0; queue q; q.push(s); while(q.size()) { s = q.front(); q.pop(); //view(s); int t = m[s]; if(s == 0) { cout << t << endl; return 0; } for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { ll ns = f(s, x, y); //view(ns); if(m.count(ns) == 0) { m[ns] = t + 1; q.push(ns); } } } } if(m.count(0)) { cout << m[0] << endl; } else { cout << "Impossible" << endl; } }