#include "bits/stdc++.h" #define MAXN 250009 #define INF 1000000007 #define mp(x,y) make_pair(x,y) #define all(v) v.begin(),v.end() #define pb(x) push_back(x) #define wr cout<<"----------------"<<endl; #define ppb() pop_back() #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++) #define ff first #define ss second #define my_little_dodge 46 #define debug(x) cerr<< #x <<" = "<< x<<endl; using namespace std; typedef long long ll; typedef pair<int,int> PII; template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} int fup[MAXN],tin[MAXN],TIM,ans; vector<int>adj[MAXN]; stack<int>st; char arr[505][505]; void dfs(int nd,int pr){ st.push(nd); fup[nd]=tin[nd]=++TIM; for(int i=0;i<int(adj[nd].size());i++){ int to=adj[nd][i]; if(to==pr) continue; if(!tin[to]){ dfs(to,nd); umin(fup[nd],fup[to]); } else umin(fup[nd],tin[to]); } if(fup[nd]==tin[nd]){ int aux,sz=0; do{ aux=st.top();st.pop(); sz++; }while(aux!=nd); ans+=sz==1; } } int main(){ //~ freopen("file.in", "r", stdin); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf(" %c",&arr[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ if(arr[i][j]=='0') continue; if(i>1 and arr[i-1][j]=='1'){ adj[(i-2)*m+j].pb((i-1)*m+j); adj[(i-1)*m+j].pb((i-2)*m+j); } if(j>1 and arr[i][j-1]=='1'){ adj[(i-1)*m+j-1].pb((i-1)*m+j); adj[(i-1)*m+j].pb((i-1)*m+j-1); } } for(int i=1;i<=n*m;i++) if(!tin[i]) dfs(i,-1); printf("%d\n",n*m-ans); return 0; }