#include #include using namespace std; int n,m,i,j,cnt,g[505][505],x,y,sol; char a[505][505]; bool viz[505][505]; deque > q; int main() { //freopen("test.in","r",stdin); //freopen("test.out","w",stdout); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%s",a[i]+1); for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='1') { cnt=0; if(a[i-1][j]=='1') cnt++; if(a[i][j-1]=='1') cnt++; if(a[i][j+1]=='1') cnt++; if(a[i+1][j]=='1') cnt++; if(cnt<=1) q.push_back(make_pair(i,j)); g[i][j]=cnt; } while(!q.empty()) { i=q.front().first; j=q.front().second; q.pop_front(); g[i][j]=0; if(a[i-1][j]=='1' && !viz[i-1][j]) { g[i-1][j]--; if(g[i-1][j]<=1) { viz[i-1][j]=1; q.push_back(make_pair(i-1,j)); } } if(a[i+1][j]=='1' && !viz[i+1][j]) { g[i+1][j]--; if(g[i+1][j]<=1) { viz[i+1][j]=1; q.push_back(make_pair(i+1,j)); } } if(a[i][j-1]=='1' && !viz[i][j-1]) { g[i][j-1]--; if(g[i][j-1]<=1) { viz[i][j-1]=1; q.push_back(make_pair(i,j-1)); } } if(a[i][j+1]=='1' && !viz[i][j+1]) { g[i][j+1]--; if(g[i][j+1]<=1) { viz[i][j+1]=1; q.push_back(make_pair(i,j+1)); } } } for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(g[i][j]>=2) sol++; printf("%d\n",sol); return 0; }