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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	while(cin >> w >> h,w){
		memset(state, 0, sizeof(state));
		memset(reached, false, sizeof(reached));
		for(int i=1; i<=h; i++) {
			for(int j=1; j<=w; j++) {
				cin >> state[i][j];
			}
		}
		int count=0;
		for(int i=1; i<=w; i++) {
			for(int j=1; j<=h; j++) {
				if(reached[i][j]==true || state[i][j] == 0)
					continue;
				else{
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <iostream>
#define MAXSIZE 50

using namespace std;

int cnt;
int island[MAXSIZE][MAXSIZE];

int check(int x, int y, int w, int h);

int main(int argc, char const *argv[])
{
	int w, h;

	while(cin >> w >> h, w | h)
	{
		for(int i = 0; i < h; i++)
		{
			for(int j = 0; j < w; j++)
			{
				cin >> island[i][j];
			}
		}

		/* processor */	
		cnt = 0;

		for(int x = 0; x < w; x++)
		{
			for(int y = 0; y < h; y++)
			{
				if(check(x, y, w, h))
				{
					cnt++;
				}
			}
		}

		cout << cnt << endl;
	}	

	return 0;
}

int check(int x, int y, int w, int h)
{
	int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

	if(island[y][x])
	{
		island[y][x] = 0;

		for(int i = 0; i < 8; i++)
		{
			if(x + dx[i] < 0 || x + dx[i] >= w || y + dy[i] < 0 || y + dy[i] >= h)
			{
				continue;
			}

			check(x + dx[i], y + dy[i], w, h);
		}

		return 1;
	}

	return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> W>>H;
	while (H) {
		N = 0;
		vector<vector<int>>field(H + 2, vector<int>(W + 2, 0));
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				cin >> field[i][j];
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				queue<pair<int, int>>Q;
				if (field[i][j]) {
					field[i][j] = 0;
					Q.push({ i,j });
					N++;
					while (!Q.empty()) {
						int cy = Q.front().first;
						int cx = Q.front().second;
						//cout << N << " " << cy << " " << cx << endl;
						Q.pop();
						
						for (int k = -1; k <= 1; k++) {
							for (int l = -1; l <= 1; l++) {
								if (field[cy + k][cx + l]) {
									field[cy + k][cx + l] = 0;
									Q.push({ cy + k,cx + l });
								}
							}
						}
					}
				}
			}
		}
		cout << N << endl;
		cin >> W >> H;
	}
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int c[100][100];//0　海
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
      cin>>w>>h;
      if(w==0&&h==0) break;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          cin>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

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

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

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

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