Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include<iostream>
#include<queue>
#include<algorithm>

using namespace std;

void sink(int i, int j,int w,int h, int** field);

int main() {
	int w, h;
	int** field;
	while (1) {
		cin >> w >> h;
		if (w == 0)break;
		field = new int* [h];
		for (int i = 0;i < h;i++) {
			field[i] = new int[w];
			for (int j = 0;j < w;j++) {
				cin >> field[i][j];
			}
		}
		int cnt = 0;
		for (int i = 0;i < h;i++) {
			for (int j = 0;j < w;j++) {
				if (field[i][j]) {
					cnt++;
					sink(i, j,w,h, field);
				}
			}
		}
		cout << cnt << endl;
	}
}

void sink(int i, int j,int w,int h, int** field) {
	field[i][j] = 0;
	if (i > 0) {
		if (field[i - 1][j])sink(i - 1, j, w, h, field);
	}
	if (i < h-1) {
		if (field[i + 1][j])sink(i + 1, j, w, h, field);
	}
	if (j > 0) {
		if (field[i][j-1])sink(i , j-1, w, h, field);
	}
	if (j < w-1) {
		if (field[i][j+1])sink(i , j+1, w, h, field);
	}
	if (i > 0 && j > 0) {
		if (field[i - 1][j - 1])sink(i - 1, j - 1, w, h, field);
	}
	if (i > 0 && j < w - 1) {
		if (field[i - 1][j + 1])sink(i - 1, j + 1, w, h, field);
	}
	if (i < h-1 && j > 0) {
		if (field[i + 1][j - 1])sink(i + 1, j - 1, w, h, field);
	}
	if (i < h-1 && j < w-1) {
		if (field[i + 1][j + 1])sink(i + 1, j + 1, w, h, field);
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
int c[50][50];
bool visited[50][50];
int vy[8] = {-1,-1,0,1,1,1,0,-1};
int vx[8] = {0,1,1,1,0,-1,-1,-1};
void calc(int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return;
  if(visited[y][x] || c[y][x] == 0) return;
  visited[y][x] = true;
  FOR(i,0,8) calc(y+vy[i],x+vx[i]);
  return;
}
int main()
{
  while(cin >> W >> H, (W || H)) {
    CLR(visited);
    FOR(y,0,H) FOR(x,0,W) cin >> c[y][x];
    int ans = 0;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(visited[y][x] || c[y][x] == 0) continue;
        calc(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int w, h;
bool state[51][51];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

void dfs(int x, int y){
    state[x][y] = 0;
    
    for(int i = 0; i < 8; i++){
        int nx = x + dx[i], ny = y + dy[i];
        if(isRegion(nx, ny) && state[nx][ny]){
            dfs(nx, ny);
        }
    }
}

int main(int argc, const char * argv[]) {
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                cin >> state[x][y];
            }
        }
        
        int cnt = 0;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                if(state[x][y]){
                    dfs(x, y);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
}

C++: #include <cstdio>
#include <stack>
#include <utility>

using namespace std;

typedef pair<int, int> point;

int w, h;
int c[50][50];
const int d[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int count()
{
    stack<point> s;
    bool v[50][50] = {};
    int n = 0;

    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (v[x][y] || c[x][y] == 0)
                continue;

            s.push(point(x, y));
            v[x][y] = true;
            ++n;
            while (!s.empty()) {
                int x = s.top().first, y = s.top().second;
                s.pop();

                for (int i = 0; i < 8; ++i) {
                    int xx = x + d[i][0], yy = y + d[i][1];
                    if (0 <= xx && xx < w && 0 <= yy && yy < h
                            && !v[xx][yy] && c[xx][yy] == 1) {
                        s.push(point(xx, yy));
                        v[xx][yy] = true;
                    }
                }
            }
        }
    }

    return n;
}

int main()
{
    for (;;) {
        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0)
            break;

        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j)
                scanf("%d", &c[j][i]);

        printf("%d\n", count());
    }

    return 0;
}