#include #include #include using namespace std; int H,W; int ans=0; vector> indexList; bool check(vector> &A,int I,int J,int &index) { if (A[I][J]==1){ A[I][J]=index; if (A[I-1][J]==1){ return check(A,I-1,J,index); } if (A[I][J-1]==1){ return check(A,I,J-1,index); } if (A[I][J+1]==1){ return check(A,I,J+1,index); } if (A[I+1][J]==1){ return check(A,I+1,J,index); } } return false; } void setIndex(vector> &A,int index) { bool loop=true; pair Index; int i,j; while (loop){ loop=false; for (int x=0;x0){ if (A[i][j]==index || A[i-1][j]==index || A[i][j-1]==index || A[i][j+1]==index || A[i+1][j]==index){ if (A[i][j]>0 && A[i][j]!=index ){ A[i][j]=index; loop=true; } if (A[i-1][j]>0 && A[i-1][j]!=index ){ A[i-1][j]=index; loop=true; } if (A[i][j-1]>0 && A[i][j-1]!=index ){ A[i][j-1]=index; loop=true; } if (A[i][j+1]>0 && A[i][j+1]!=index ){ A[i][j+1]=index; loop=true; } if (A[i+1][j]>0 && A[i+1][j]!=index ){ A[i+1][j]=index; loop=true; } } } } } } int main(int argc, char* argv[]) { cin>>H>>W; int n0=0,n1=0; vector> A(H+2,vector(W+2)); queue> q; int i,j; for (i=1;i<=H;i++){ for (j=1;j<=W;j++){ cin>>A[i][j]; if (A[i][j]==0){ n0++; }else{ q.push(make_pair(i,j)); indexList.push_back(make_pair(i,j)); n1++; } } } if (n0==H*W){ cout<<0< p; p=q.front(); q.pop(); I=p.first; J=p.second; if (A[I][J]==1){ check(A,I,J,index); setIndex(A,index); index++; ans++; } } cout<