#include<bits/stdc++.h>
using namespace std;
char a[1000][1000];
bool vis[1000][1000];
int ans = 0;
int n,m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
struct Node{
int x;
int y;
}q[10000];
void bfs(int x,int y){
int head = 1;
int tail = 1;
vis[x][y] = true;
q[tail].x = x;
q[tail].y = y;
tail++;
while(head < tail){
int x0 = q[head].x;
int y0 = q[head].y;
for(int i = 0;i < 4;i++){
int nx = x0 + dx[i];
int ny = y0 + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && a[nx][ny] == '0'){
q[tail].x = nx;
q[tail].y = ny;
tail++;
vis[nx][ny] = true;
}
}
head ++;
}
}
int main() {
cin >> n >> m;
for(int i = 0;i < n;i++)
cin >> a[i];
for(int j = 0;j < m;j++)
if(!vis[0][j] && a[0][j] == '0')
bfs(0,j);
for(int j = 0;j < m;j++)
if(!vis[n - 1][j] && a[n - 1][j] == '0')
bfs(n - 1,j);
for(int j = 0;j < n;j++)
if(!vis[j][0] && a[j][0] == '0')
bfs(j,0);
for(int j = 0;j < n;j++)
if(!vis[j][m - 1] && a[j][m - 1] == '0')
bfs(j,m - 1);
for(int i = 1;i < n - 1;i++){
for(int j = 1;j < m - 1;j++){
if(!vis[i][j] && a[i][j] == '0'){
bfs(i,j);
ans++;
}
}
}
cout << ans;
return 0;
}