输入

6 7
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0

输出

4
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <list>
#include <queue>


using namespace std;

const int maxn=100;


struct node{

    int x,y;        //坐标 


}Node;


int n,m;            //矩阵大小 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)     //判断坐标是否需要访问 
{
    //越界返回false
    if(x>=n || x<0 || y>=m || y<0)
    {
        return false;
    }

    //当前的位置为0(游戏设定的禁止落脚点),
    //或者坐标点已经入过队列,返回false

    if( matrix[x][y]==0 || inq[x][y]==true  )
     {
        return false;

     }

     return true;



}




//bfs 函数访问坐标所在的块
//其实就是访问一个点,就实现访问了一整块的作用
//并且把一整块的坐标点都加了 入队标记


void BFS(int x,int y)
{

    queue<node> q;      //定义队列 

    Node.x  =x;
    Node.y  =y;


    q.push(Node);   //将第一个点的坐标入队。 


    inq[x][y]=true;


    while(!q.empty())
    {
        node top=q.front();     //取出队首元素

        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))
            {

                Node.x=newX;
                Node.y=newY;

                q.push(Node);
                inq[newX][newY] = true;



            }






        }







    }










} 














int main()
{

    scanf("%d %d",&n,&m);

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {

            scanf("%d",&matrix[i][j]);      //读入01矩阵 



        }




    }


    int ans=0;      //记录存放块数

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            //如果元素为1且未入过队

            if( matrix[i][j]==1 && inq[i][j]==false)
            {
                ans++;

                BFS(i,j);   //访问整个块,将该块的所有“1”标记为 true 



            } 



        }





    } 






    printf("%d",ans);





    return 0;


}