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<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll H, W;
ll field[110][110];
ll dx[8] = { 0, 1, 0,-1, 1, 1,-1,-1 };
ll dy[8] = { 1, 0,-1, 0, 1,-1, 1,-1 };
void dfs(ll y, ll x) {
	field[y][x] = 0;
	for (ll i = 0; i < 8; i++) {
		ll ny = y + dy[i];
		ll nx = x + dx[i];
		if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
			if (field[ny][nx] == 1)
				dfs(ny, nx);
		}
	}
}
int main() {
	while (cin >> H >> W) {
		if (H == 0 && W == 0) break;
		ll cnt = 0;
		for (ll i = 0; i < W; i++) {
			for (ll j = 0; j < H; j++) {
				cin >> field[i][j];
			}
		}
		for (ll y = 0; y < W; y++) {
			for (ll x = 0; x < H; x++) {
				if (field[y][x] == 1) {
					dfs(y, x);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
}

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

int M[52][52];

void fill(int y, int x){
	M[y][x] = 0;
	if(M[y-1][x]) fill(y-1,x);
	if(M[y+1][x]) fill(y+1,x);
	if(M[y][x-1]) fill(y,x-1);
	if(M[y][x+1]) fill(y,x+1);
	if(M[y-1][x-1]) fill(y-1,x-1);
	if(M[y+1][x-1]) fill(y+1,x-1);
	if(M[y-1][x+1]) fill(y-1,x+1);
	if(M[y+1][x+1]) fill(y+1,x+1);
}

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

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i = 0; i<n; i++)
typedef long long ll;
using namespace std;
typedef pair<int,int> P;

int w,h,sum=0,umi[52][52];

int kesi(int a,int b){
  umi[a][b]=0;
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      if(umi[a+dx][b+dy]==1){
        kesi(a+dx,b+dy);
      }
    }
  }
  return 0;
}

int main(){
  while(cin >> w >> h&&w&&h){
    r(i,h){
      r(j,w){
        cin >> umi[i][j];
      }
    }
    r(i,h){
      r(j,w){
        if(umi[i][j]==1){
          sum++;
          kesi(i,j);
        }
      }
    }
    cout<<sum<<endl;
    sum=0;
  }
}

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

using namespace std;

	ifstream fin;
	ofstream fout;

	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

	int gomi = 0;

	int a[52][52] = {};
	void check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		int width = 0, height = 0;
		int count = 0;

		while (true){

			cin >> width >> height;
			if (width == 0 && height == 0)break;

			//初期化
			count = 0;
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			for (int i = 0; i < height; i++)
				for (int j = 0; j < width; j++)
					cin >> a[i + 1][j + 1];

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
						count++;
					}
				}
			}
			cout << count << endl;
			fout << count << endl;
		}
	}

	void check(int y, int x){
		for (int i = -1; i <= 1; i++)
			for (int j = -1; j <= 1; j++)
				if (i + j != 0 || i - j != 0)
					if (a[y + i][x + j] == 1){
			a[y + i][x + j] = 0;
			check(y + i, x + j);
					}
	}
//------------------------------------------------------------↑↓どちらでもいい------------------------------------------------------------
/*	void check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
	}*/

C++: #include<iostream>
#include <malloc.h>
#include <vector>
using namespace std;

void search(int i,int j,int **c,int h,int w){
  c[i][j] = 0;
  if(j<w-1 && c[i][j+1]==1) search(i,j+1,c,h,w);
  if(i<h-1 && c[i+1][j]==1) search(i+1,j,c,h,w);

  if(j>0 && i<h-1 && c[i+1][j-1]==1) search(i+1,j-1,c,h,w);
  if(j<w-1 && i>0 && c[i-1][j+1]==1) search(i-1,j+1,c,h,w);
  if(j>0 && i>0 && c[i-1][j-1]==1) search(i-1,j-1,c,h,w);
  if(j<w-1 && i<h-1 && c[i+1][j+1]==1) search(i+1,j+1,c,h,w);
  if(i>0 && c[i-1][j]==1) search(i-1,j,c,h,w);
  if(j>0 && c[i][j-1]==1) search(i,j-1,c,h,w);
}

int main(){
  vector<int> vec;
  while(1){
    int w,h;
    cin >> w >> h;
    if(w==0 && h==0) break;
    int **c = (int**)malloc(sizeof(int*)*h);
    for(int i=0;i<h;i++){
        c[i] = (int*)malloc(sizeof(int*)*w);
        for(int j=0;j<w;j++) cin >> c[i][j];
    }
    int count = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(c[i][j]==1){
          count += 1;
          search(i,j,c,h,w);
        }
      }
    }
    //cout << count << endl;
    vec.push_back(count);
    for(int i=0;i<h;i++){
        free(c[i]);
    }
    free(c);
  }
  int n = vec.size();
  for(int i=0;i<n;i++) cout << vec[i] << endl;
  return 0;
}