#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
using namespace std;
#define maxn 100
struct node
{
int x, y;
};
node Node;
int n, m;
int matrix[maxn][maxn];
bool inq[maxn][maxn] = { false };
int X[4] = {0,0,1,-1};
int Y[4] = {1,-1,0,0};
bool judge(int x, int y)//函数用于判断什么样的元素需要压入队列中
{
if (x >= n || x < 0 || y >= m || y < 0)
return false;
if (inq[x][y] == true||matrix[x][y] == 0)
return false;
return true;
}
void BFS(int x, int y)
{
queue<node> q;
Node.x = x;
Node.y = y;
q.push(Node);//stl容器中的queue:压入的是一个副本而非引用,改版x或y不会影响压入的数据
inq[x][y] = true;//dont forget the judge flag
while (!q.empty())
{
node top = q.front();//you can directlt use "="
q.pop();
for (int i = 0; i < 4; i++)
{
int newx = top.x + X[i];
int newy = top.y + Y[i];
if (judge(newx, newy) == true)
{
Node.x = newx;
Node.y = newy;
q.push(Node);//注意:每入队一个元素,其标记就改变。
inq[newx][newy] = true;
}
}
}
}
int main()
{
cin >> n >> m;
for(int x = 0;x < n;x++)
for (int y = 0; y < m; y++)
{
cin >> matrix[x][y];
}
int num = 0;
for (int x = 0; x < n; x++)
for (int y = 0; y < m; y++)
{
if (matrix[x][y] == 1 && inq[x][y] == false)
{
num++;
BFS(x, y);
}
}
cout << num<<endl;
}