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>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

vector<pair<ll, ll>> d = {
  {-1, -1},
  {+1, -1},
  {-1, +1},
  {+1, +1},
  {+0, +1},
  {+0, -1},
  {+1, +0},
  {-1, +0},
};

void dfs(ll x, ll y, vector<vector<ll>> &graph) {
  graph[y][x] = 0;
  for(auto v: d) {
    ll nx = v.first + x;
    ll ny = v.second + y;
    if (0 <= ny && ny < graph.size() && 0 <= nx && nx < graph[y].size() && graph[ny][nx]) {
      dfs(nx, ny, graph);
    }
  }
}

bool solve() {
  ll input(w, h);
  if (w == 0 && h == 0) return false;

  vector<vector<ll>> graph(h);
  REP(i, h) {
    vector<ll> r(w);
    REP(j, w) cin >> r[j];
    graph[i] = r;
  }

  ll cnt = 0;
  REP(i, h) REP(j, w) {
    if (graph[i][j]) {
      dfs(j, i, graph);
      cnt++;
    }
  }
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve());
}

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;

}

LL exp(LL a, LL b){
    LL sum = a;
    LL res = 1;
    while(b){
        if(b&1){
            res = (res * sum)%MOD;
        }
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int N, M;
int mx[]={1, 1, 1, 0, -1, -1, -1, 0}, my[]={1, 0, -1, -1, -1, 0, 1, 1};

int main(){
    while(cin>>M>>N&&N){
        vector<vector<int> > m(N+2, vector<int>(M+2, 0));
        REPA(i, N+1){
            REPA(j, M+1){
                cin >> m[i][j];
            }
        }
        int ans = 0;
        REPA(i, N+1){
            REPA(j, M+1){
                if(!m[i][j])continue;
                ++ans;
                queue<PII> que;
                que.push(PII(i, j));
                m[i][j]=0;
                while(!que.empty()){
                    PII d = que.front();
                    que.pop();
                    REP(i, 8){
                        int nx = d.first + mx[i];
                        int ny = d.second + my[i];
                        if(!m[nx][ny])continue;
                        m[nx][ny]=0;
                        que.push(PII(nx, ny));
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

int w, h;
int c[51][51];
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

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

int solve(){
	int res = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(c[i][j] == 1){
				dfs(i, j);
				res++;
			}
		}
	}

	return res;
}

int main(void){
	while(scanf("%d%d", &w, &h) != EOF){
		if(w == 0 and h == 0) break;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &(c[i][j]));
			}
		}
		printf("%d\n", solve());
	}

	return 0;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e9
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main() {
	while (true) {
		int w, h;
		cin >> w >> h;
		if (w == 0) return 0;
		int gogo[51][51] = {};
		REP(i, h) {
			REP(q, w) {
				cin >> gogo[i][q];
			}
		}
		int xe[8] = { -1,-1,-1,0,0,1,1,1 };
		int ye[8] = { 1,0,-1,1,-1,1,0,-1 };
		queue<pair<int, int>> next;
		int visited[51][51] = {};
		int ans = 0;
		REP(i, h) {
			REP(q, w) {
				if (visited[i][q] == 0&&gogo[i][q] == 1) {
					visited[i][q] = 1;
					ans++;
					next.push(make_pair(i, q));
					while (next.empty() == false) {
						pair<int, int> now = next.front();
						next.pop();
						REP(q, 8) {
							int now_x = now.first + xe[q];
							int now_y = now.second + ye[q];
							if (now_x >= 0 && now_x < h && now_y >= 0 && now_y < w) {
								if (visited[now_x][now_y] == 0 && gogo[now_x][now_y] == 1) {
									visited[now_x][now_y] = 1;
									next.push(make_pair(now_x, now_y));
								}
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}