Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<int> > c;
vector<vector<bool> > done;
int w,h;
int dy[] = {-1,-1,-1,0,0,1,1,1};
int dx[] = {1,0,-1,1,-1,1,0,-1};

void dfs(int y, int x)
{
		done[y][x] = true;
		for(int i=0;i<8;i++)
		{
				int ny = y + dy[i];
				int nx = x + dx[i];
				if(0<=ny && ny<h && 0<=nx && nx<w && c[ny][nx]==1 && !done[ny][nx])
				{
						dfs(ny,nx);
				}
		}
}

signed
main()
{
	while(cin >> w >> h and !(w==0 & h==0))
	{
			c = vector<vector<int> >(h,vector<int>(w));
			done = vector<vector<bool> >(h,vector<bool>(w, false));
			for(int y=0;y<h;y++)
					for(int x=0;x<w;x++)
							cin >> c[y][x];

			int cnt = 0;
			for(int y=0;y<h;y++)
			{
					for(int x=0;x<w;x++)
					{
							if(c[y][x] && !done[y][x]){
									cnt++;
									dfs(y,x);
							}
					}
			}
			cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {0,1,-1,1,-1,0,1,-1};
bool visited[100][100];
int w,h;
int f[100][100];
void dfs(int x,int y){
  visited[y][x] = true;
  for(int i=0;i<8;++i){
    int mx=x+dx[i];
    int my=y+dy[i];
    if(mx>=w||my>=h||mx<0||my<0) continue;
    if(visited[my][mx] || !f[my][mx]) continue;
    dfs(mx,my);
  }
}

int main(){
  while(cin>>w>>h,w||h){
    memset(visited,0,sizeof(visited));
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j) cin >> f[i][j];
    int cnt = 0;
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j){
	if(f[i][j]&&!visited[i][j]){
	  ++cnt;
	  dfs(j,i);
	}
      }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int w, h;
//?§???????
const int mx[] = {-1, 0, 1, -1, 1, -1, 0, 1 };
const int my[] = { 1, 1, 1, 0, 0, -1, -1, -1};
vector<int> vx(begin(mx), end(mx));
vector<int> vy(begin(my), end(my));
vector<vector<int>> area;
int islands = 0;

//????±????????????¨????????¢?´¢
void dfs(int i,int j){
	for (int n = 0; n < vx.size(); n++){
		int x = j + vx[n];
		int y = i + vy[n];
		if (x >= 0 && x < w &&
			y >= 0 && y < h &&
			area[y][x] == 1){
			area[y][x] = 2;
			dfs(y,x);
		}
	}
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//?????????
		islands = 0;
		area.clear();
		area = vector<vector<int>>(h, vector<int>(w));
		//??\???
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}
		//??¨??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (area[i][j] == 1){
					islands++;
					area[i][j] = 2;
					dfs(i, j);
				}
			}
		}
		cout << islands << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#include<vector>

int wm,hm;
vector< vector<int> > wh(51, vector<int>(51,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int y,int x){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[ny][nx]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[ny][nx]=0;
      dfs(ny,nx);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=aa;
    hm=bb;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

int dx[8] = { 1, 1, 1, 0, 0, -1, -1, -1 };
int dy[8] = { 1, 0, -1, 1, -1, 1, 0, -1 };

int field[50][50] = {};

int w, h;

void dfs( int x, int y );

int main() {

	while( cin >> w >> h, w ) {
		REP( i, h ) {
			REP( j, w ) {
				cin >> field[j][i];
			}
		}

		int cnt = 0;
		REP( i, h ) {
			REP( j, w ) {
				if( field[j][i] == 1 ) {
					cnt++;
					dfs( j, i );
				}
			}
		}

		cout << cnt << endl;

	}

}

void dfs( int x, int y ) {
	field[x][y] = 0;
	REP( i, 8 ) {
		if( x + dx[i] >= 0 && x + dx[i] < w &&
			y + dy[i] >= 0 && y + dy[i] < h &&
			field[x + dx[i]][y + dy[i]] == 1 ) {

			dfs( x + dx[i], y + dy[i] );

		}

	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long double ld;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

int c[55][55];
bool v[55][55];

void solve(){

    int w,h;
    vector<int> a;

    while(true){
        cin >> w >> h;
        if(w==0&&h==0) break;
        rep(i,h){
            rep(j,w){
                v[i][j]=false;
            }
        }
        rep(i,h) v[i][w]=true;
        rep(j,w) v[h][j]=true;
        v[h][w]=true;

        rep(i,h){
            rep(j,w){
                cin >> c[i][j]; 
                if(c[i][j]==0) v[i][j]=true;
            }
        }

        int ans=0;
            
            rep(i,h){
                rep(j,w){
                    if(!v[i][j]) {
                        stack<pair<int,int>> st;
                        st.push(make_pair(i,j));
                        while(!st.empty()){
                            int ti = st.top().first;
                            int tj = st.top().second;
                            st.pop();
                            v[ti][tj]=true;
                            
                            if(v[ti+1][tj]==false) st.push(make_pair(ti+1,tj));
                            if(v[ti][tj+1]==false) st.push(make_pair(ti,tj+1));
                            if(ti>0) {if(v[ti-1][tj]==false) st.push(make_pair(ti-1,tj));}
                            if(tj>0) {if(v[ti][tj-1]==false) st.push(make_pair(ti,tj-1));}
                            if(v[ti+1][tj+1]==false) st.push(make_pair(ti+1,tj+1));
                            if(ti>0) {if(v[ti-1][tj+1]==false) st.push(make_pair(ti-1,tj+1));}
                            if(tj>0) {if(v[ti+1][tj-1]==false) st.push(make_pair(ti+1,tj-1));}
                            if(ti>0&&tj>0) {if(v[ti-1][tj-1]==false) st.push(make_pair(ti-1,tj-1));}
                            
                        }
                        ans++;
                    }
                }
            }

        a.emplace_back(ans);
        


    }

    for(auto e:a){
        cout << e << endl;
    }

}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}