HTML & JS
    #include <iostream>
    #include <vector>
    #include <queue>
    #include <set>
    #include <algorithm>
    using namespace std;
     
    int main() 
    {
        int N,M;
      //  cin>>N>>M;
        while(cin >> N >> M && N )
        {
            int A[N][M],i,j;
            for(i=0;i<N;i++)
            for(j=0;j<M;j++)
            cin>>A[i][j];
            
            int visited[N][M];
            for(i=0;i<N;i++)
            for(j=0;j<M;j++)
            visited[i][j]=0;
            vector<int>vec;set<int>S;
            pair<int,int>p;
            p.first=0;p.second=0;
            queue<pair<int,int>>q;int u,v,U,V,cnt=0;
            
          /*  for(i=0;i<N;i++){
            for(j=0;j<M;j++){
            cout<<A[i][j]<<" ";}
            cout<<endl;}*/
            for(i=0;i<N;i++)
            {
                for(j=0;j<M;j++)
                {
                    if((A[i][j]==1)and(visited[i][j]==0))
                    {
                        cnt=0;
                        visited[i][j]=1;
                        p.first=i;p.second=j;
                        q.push(p);
                        while(!q.empty())
                        {
                            p=q.front();
                            u=p.first+1;//down
                            v=p.second;//down
                            U=p.first;//right
                            V=p.second+1;//right
                            q.pop();
                            if((visited[u][v]==0)and(A[u][v]==1)and(u<N)and(v<M))
                            {
                                visited[u][v]=1;
                                cnt++;
                                q.push(make_pair(u,v));
                            }
                            if((visited[U][V]==0)and(A[U][V]==1)and(U<N)and(V<M))
                            {
                                visited[U][V]=1;
                                cnt++;
                                q.push(make_pair(U,V));
                            }
                        }
                        S.insert(cnt+1);
                        vec.push_back(cnt+1);
                    }
                }
            }
            
            cout<<vec.size()<<endl;
            set<int>::iterator k;
            for(k=S.begin();k!=S.end();k++)
            {
                cout<<*k<<" "<<count(vec.begin(),vec.end(),*k)<<endl;
            }
            
            
            
            
          //  cin>>N>>M;
        }
        
    	 
    	return 0;
    } 

Generated URL:

Output:

No Output
Report Bug