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>
#include <vector>
using namespace std;

int c[50][50];
bool visited[50][50] = {false};
int w, h;


void search(int x, int y){
    if(x < 0 || x >= w || y < 0 || y >= h || c[y][x] == 0){
        return;
    }
    if(visited[y][x] == true){
        return;
    }

    visited[y][x] = true;

    search(x-1, y);
    search(x+1, y);
    search(x, y-1);
    search(x, y+1);
    search(x+1, y+1);
    search(x+1, y-1);
    search(x-1, y+1);
    search(x-1, y-1);
}

void init(){
    for(int y = 0; y < 50; y++){
        for(int x = 0; x < 50; x++){
            visited[y][x] = false;
        }
    }
}


int main(){
    int count;
    int n = 0;
    vector<int> ans;

    while(1){
        cin >> w >> h;

        if(w == 0 and h == 0){
            break;
        }

        n++;
        count = 0;

        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                cin >> c[y][x];
            }
        }

        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                if(c[y][x] == 1 and visited[y][x] == false){
                    search(x, y);
                    count++;
                }
            }
        }

        ans.push_back(count);
        
        init();
    }

    for(int j = 0; j < n; j++){
        printf("%d\n", ans[j]);
    }

    return 0;

}

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

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

int map[51][51];
int w, h;

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (x < 0 || w <= x) continue;
		if (y < 0 || w <= y) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
	return 0;
}

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

int main() {

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

  long long int w, h;

  while( true ) {

    cin >> w >> h;

    if ( w == 0 ) break;

    int map[52][52] = {0};

    for ( int y = 0; y < h; y++ ) {
      for ( int x = 0; x < w; x++ ) {

	cin >> map[x+1][y+1];

      }
    }

    int ans = 0;

    while( true ) {

      bool flag = false;
      for ( int y = 1; y < h + 1; y++ ) {
	for ( int x = 1; x < w + 1; x++ ) {

	  if ( map[x][y] == 1 ) {
	    queue<int> qx, qy;
	    qx.push(x);
	    qy.push(y);
	    map[x][y] = 0;
	    while( qx.size() > 0 ) {

	      int lx = qx.front();
	      int ly = qy.front();
	      for ( int i = 0; i < 8; i++ ) {
		if ( map[lx+dx[i]][ly+dy[i]] == 1 ) {
		  map[lx+dx[i]][ly+dy[i]] = 0;
		  qx.push(lx+dx[i]);
		  qy.push(ly+dy[i]);
		}
	      }
	      qx.pop();
	      qy.pop();

	    }
	    flag = true;
	    break;
	  }

	}
	if ( flag ) break;
      }
      if ( flag == false ) break;

      ans++;

    }

    cout << ans << endl;

  }

  return 0;

}

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

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

void solve(int x,int y){
	if(map[y][x] == 1){
		map[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		if(w==0&&h==0){
			break;
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> map[i][j];
			}
		}

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

		cout << count << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>

using namespace std;

class Solver {
public:
	static void solve() {
		int w, h;
		cin >> w >> h;
		vector<int> res;
		while (w > 0 && h > 0) {
			vector<vector<bool>> grid(h, vector<bool>(w));
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					int now;
					cin >> now;
					grid[i][j] = now;
				}
			}

			int cnt = 0;
			vector<vector<int>> visited(h, vector<int>(w, false));

			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!grid[i][j] || visited[i][j])continue;

					cnt++;

					queue<tuple<int, int>> que;
					que.push(make_tuple(i, j));
					while (!que.empty()) {
						auto pos = que.front();
						que.pop();
						int y = get<0>(pos);
						int x = get<1>(pos);
						if (y < 0 || h <= y || x < 0 || w <= x
							|| !grid[y][x] || visited[y][x])continue;

						visited[y][x] = true;

						que.push(make_tuple(y - 1, x));
						que.push(make_tuple(y + 1, x));
						que.push(make_tuple(y, x - 1));
						que.push(make_tuple(y, x + 1));
						que.push(make_tuple(y - 1, x - 1));
						que.push(make_tuple(y + 1, x - 1));
						que.push(make_tuple(y - 1, x + 1));
						que.push(make_tuple(y + 1, x + 1));
					}
				}
			}

			res.emplace_back(cnt);

			cin >> w >> h;
		}

		for (int i = 0; i < res.size(); i++) {
			cout << res[i] << endl;
		}
	}
};

int main() {
	Solver::solve();
}