Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

int W, H;

int field[52][52];
bool visited[52][52];
int tx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int ty[] = {1, 0, -1, -1, -1, 0, 1, 1};

void island(int y, int x)
{
  visited[y][x] = true;
  for (int i = 0; i < 8; i++) {
    if (!visited[y + ty[i]][x + tx[i]] && field[y + ty[i]][x + tx[i]] == 1) {
      island(y + ty[i], x + tx[i]);
    }
  }
}

void solve()
{
  fill_n(&visited[0][0], 52 * 52, false);
  int c = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (!visited[i][j] && field[i][j] == 1) {
	island(i, j);
	c++;
      }
    }
  }
  printf("%d\n", c);
}

int main()
{
  while (scanf("%d %d ", &W, &H) == 2) {
    if (W == 0 && H == 0) {
      return 0;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
	scanf("%d ", &field[i][j]);
      }
    }
    solve();
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}



int main() {
	int w, h;
	int dx[8] = { -1, 0 , 1,-1,1,-1,0,1};
	int dy[8] = { -1,-1 ,-1, 0,0, 1,1,1};
	int input;
	while (1) {
		cin >> w >> h;
		if (w == 0) break;
		vector<vector<int>> field(h + 2, vector<int>(w + 2, 2));
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				cin >> input;
				if (input == 1) {
					field[i][j] = 1;
				}
				else {
					field[i][j] = 0;
				}
			}
		}
		/*
		rep(i, h + 2) {
			rep(j, w + 2) {
				cout << field[i][j] << " " ;
			}
			cout << endl;
		}
		*/
		int ans = 0;
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				if (field[i][j] == 1) {
					queue<pair<int, int>> que;
					que.emplace(i, j);
					ans++;
					while (!que.empty()) {
						pair<int, int> now = que.front();
						que.pop();
						if (field[now.first][now.second] == 0) continue;
						field[now.first][now.second] = 0;
						rep(k, 8) {
							if (field[now.first + dy[k]][now.second + dx[k]] == 1) {
								que.emplace(now.first + dy[k], now.second + dx[k]);
							}

						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};
vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
int main(){
  while (1){
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0){
      break;
    }
    vector<vector<int>> c(h + 2, vector<int>(w + 2, 0));
    for (int i = 1; i <= h; i++){
      for (int j = 1; j <= w; j++){
        cin >> c[i][j];
      }
    }
    int ans = 0;
    vector<vector<bool>> used(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++){
      for (int j = 1; j <= w; j++){
        if (!used[i][j] && c[i][j] == 1){
          used[i][j] = true;
          ans++;
          queue<pair<int, int>> Q;
          Q.push(make_pair(i, j));
          while (!Q.empty()){
            int y = Q.front().first;
            int x = Q.front().second;
            Q.pop();
            for (int k = 0; k < 8; k++){
              int y2 = y + dy[k];
              int x2 = x + dx[k];
              if (!used[y2][x2] && c[y2][x2] == 1){
                used[y2][x2] = true;
                Q.push(make_pair(y2, x2));
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

using ll = long long;

class UnionFind {
public:
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    bool connect(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};
int dx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[] = {1, 0, -1, -1, -1, 0, 1, 1};

int main()
{
    int h,w;
    while(cin >> h >> w){
        if(h == 0)return 0;
        map<pair<int,int>, int> se;
        int cnt = 0;
        for(int x = 0;x < w;x++){
            for(int y = 0;y < h;y++){
                int c;cin >> c;
                if(c){
                    se[make_pair(x, y)] = cnt;
                    cnt++;
                }
            }
        }
        UnionFind Uni(cnt);
        for(auto a : se){
            int x = a.first.first;
            int y = a.first.second;
            for(int i = 0;i < 8;i++){
                if(se.find(make_pair(x + dx[i], y + dy[i])) != se.end()){
                    Uni.connect(a.second, se[make_pair(x + dx[i], y + dy[i])]);
                }
            }
        }
        set<int> s;
        for(int i = 0;i < cnt;i++){
            s.insert(Uni.root(i));
        }
        std::cout<<s.size()<<endl;
    }
}