1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=1005;
5 int n,m,a[MAX][MAX],id[MAX][MAX],b[MAX][MAX],tx,ans;
6 int fa[MAX*MAX];
7 bool vis[MAX*MAX];
8 int dx[8]={-2,-1,1,2,2,1,-1,-2};
9 int dy[8]={1,2,2,1,-1,-2,-2,-1};
10 int dfs(int x){
11 int i,j;
12 for (i=1;i<=tx;i++)
13 if (!vis[i] && b[x][i]){
14 vis[i]=true;
15 if (fa[i]==-1 || dfs(fa[i]))
16 return fa[i]=x,1;
17 }
18 return 0;
19 }
20 int main(){
21 freopen ("horse.in","r",stdin);freopen ("horse.out","w",stdout);
22 int i,j,k,x,y;
23 scanf("%d%d",&n,&m);
24 for (i=1;i<=n;i++)
25 for (j=1;j<=m;j++)
26 scanf("%d",&a[i][j]),id[i][j]=a[i][j]==0?++tx:0;
27 for (i=1;i<=n;i++)
28 for (j=1;j<=m;j++)
29 if (!a[i][j])
30 for (k=0;k<8;k++){
31 x=i+dx[k],y=j+dy[k];
32 if (x<1 || x>n || y<1 || y>m || a[x][y]) continue;
33 b[ id[i][j] ][ id[x][y] ]=1;
34 }
35 memset(fa,-1,sizeof(fa));
36 int zt=0;
37 for (i=1;i<=tx;i++){
38 memset(vis,false,sizeof(vis));
39 zt+=dfs(i);
40 }
41 printf("%d",tx-zt/2);
42 return 0;
43 }