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 <iostream>
#define MAXSIZE 50

using namespace std;

int cnt;
int island[MAXSIZE][MAXSIZE];

int check(int x, int y, int w, int h);

int main(int argc, char const *argv[])
{
	int w, h;

	while(cin >> w >> h, w | h)
	{
		for(int i = 0; i < h; i++)
		{
			for(int j = 0; j < w; j++)
			{
				cin >> island[i][j];
			}
		}

		/* processor */	
		cnt = 0;

		for(int x = 0; x < w; x++)
		{
			for(int y = 0; y < h; y++)
			{
				if(check(x, y, w, h))
				{
					cnt++;
				}
			}
		}

		cout << cnt << endl;
	}	

	return 0;
}

int check(int x, int y, int w, int h)
{
	int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

	if(island[y][x])
	{
		island[y][x] = 0;

		for(int i = 0; i < 8; i++)
		{
			if(x + dx[i] < 0 || x + dx[i] >= w || y + dy[i] < 0 || y + dy[i] >= h)
			{
				continue;
			}

			check(x + dx[i], y + dy[i], w, h);
		}

		return 1;
	}

	return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, H, W, L, R;


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

	cin >> W>>H;
	while (H) {
		N = 0;
		vector<vector<int>>field(H + 2, vector<int>(W + 2, 0));
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				cin >> field[i][j];
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				queue<pair<int, int>>Q;
				if (field[i][j]) {
					field[i][j] = 0;
					Q.push({ i,j });
					N++;
					while (!Q.empty()) {
						int cy = Q.front().first;
						int cx = Q.front().second;
						//cout << N << " " << cy << " " << cx << endl;
						Q.pop();
						
						for (int k = -1; k <= 1; k++) {
							for (int l = -1; l <= 1; l++) {
								if (field[cy + k][cx + l]) {
									field[cy + k][cx + l] = 0;
									Q.push({ cy + k,cx + l });
								}
							}
						}
					}
				}
			}
		}
		cout << N << endl;
		cin >> W >> H;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int c[100][100];//0　海
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

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

  while(true){
      cin>>w>>h;
      if(w==0&&h==0) break;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          cin>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

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

int w, h;
int c[50][50];

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

int main(){
  	cin >> w >> h;
    while(w!=0){
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> c[i][j];
            }
        }
        int ans = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(c[i][j]==1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
      	cin >> w >> h;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;
typedef long long ll;

int ans = 0;
vector <vector<int> > MAP;

int solve(int locateX,int locateY,int w,int h){
  //!
  if(locateX<0 || locateY<0 || locateX>=h || locateY>=w)return 0;
  //CHECK
  if(MAP[locateX][locateY]==1){
    MAP[locateX][locateY]=0;
    //!--
    solve(locateX,locateY+1,w,h);
    solve(locateX+1,locateY+1,w,h);
    solve(locateX+1,locateY,w,h);
    solve(locateX+1,locateY-1,w,h);
    solve(locateX,locateY-1,w,h);
    solve(locateX-1,locateY-1,w,h);
    solve(locateX-1,locateY,w,h);
    solve(locateX-1,locateY+1,w,h);
    //--!
    return 1;
  }
  return 0;
}

int ope(){
  int w,h;
  ans=0;
  MAP.resize(0);
  cin >> w >> h;
  if(!w&&!h)return 0;
  //INPUT
  for(int i=0;i<h;++i){
    vector<int> temp;
    for(int j=0;j<w;++j){
      int ti;
      cin >> ti;
      temp.push_back(ti);
    }
    MAP.push_back(temp);
  }
  //EXEC
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j)  
      ans+=solve(i,j,w,h);

  //OUTPUT
  cout << ans << endl;
  return 1;
}
int main(){
  while(ope()){}
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

using Graph = vector<vector<int>>;
//using Graph = vector<vector<Edge>>;
bool seen[100][100];
int field[100][100];

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

int w, h;

void dfs(int i, int j){
    seen[i][j] = true;
    REP(k, 8){
        if(i+dx[k] >= h || i+dx[k] < 0 || j+dy[k] >= w || j+dy[k] < 0) continue;
        if(field[i+dx[k]][j+dy[k]] == 0) continue;
        if(seen[i+dx[k]][j+dy[k]]) continue;
        dfs(i+dx[k], j+dy[k]);
    }
}

int main()
{
    for(;;){
        cin >> w >> h;
        if(w==0 && h==0)
            break;
        REP(i, h)
            REP(j, w){
                cin >> field[i][j];
                seen[i][j] = false;
            }
        int count = 0;
        REP(i, h)
            REP(j, w){
                if(seen[i][j] || field[i][j] == 0) continue;
                dfs(i, j);
                count++;
            }
        cout << count << endl;
    }

    return 0;
    
}