#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define INF 0x3f3f3f
int gcd(int a, int b) {return b==0 ? a:gcd(b,a%b);}
int n, m;
int flag;
int min_cnt;
int s[20][20];
int a[20][20];
int ans[20][20];
int vis[20][20];
int xx[] = {0,0,-1,1};
int yy[] = {-1,1,0,0};
int get_sum()
{
int cnt = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++) cnt += vis[i][j];
}
return cnt;
}
void Filp(int x, int y)
{
a[x][y] = 1-a[x][y];
for(int i = 0; i < 4; i++)
{
int tx = x+xx[i];
int ty = y+yy[i];
if(tx<1||ty<1||tx>n||ty>m) continue;
a[tx][ty] = 1-a[tx][ty];
}
}
void dfs(int c)
{
if(c==n+1){
for(int i = 1; i <= m; i++)
{
if(a[n][i]==1) return;
}
int cnt = get_sum();
if(cnt<min_cnt){
min_cnt = cnt;
flag = 1;
memcpy(ans,vis,sizeof(vis));
}
return;
}
for(int i = 1; i <= m; i++)
{
if(a[c-1][i]==1){
Filp(c,i);
vis[c][i] = 1;
}
}
dfs(c+1);
}
void init()
{
for(int i = 0; i < 1<<m; i++)
{
memset(vis,0,sizeof(vis));
memcpy(a,s,sizeof(s));
int x = i;
int k = m;
while(x!=0)
{
vis[1][k] = x%2;
if(x%2==1) Filp(1,k);
k--;
x /= 2;
}
dfs(2);
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++) scanf("%d", &s[i][j]);
}
flag = 0;
min_cnt = INF;
init();
if(!flag) printf("IMPOSSIBLE\n");
if(flag){
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
printf("%d ", ans[i][j]);
}
printf("\n");
}
}
return 0;
}
09-08