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>
using namespace std;
int a[52][52];
int sima(int i,int j,int k){
	if(a[i-1][j-1] == 1){
		a[i-1][j-1] = k;
		sima(i-1,j-1,k);
	}
	if(a[i-1][j] == 1){
		a[i-1][j] = k;
		sima(i-1,j,k);
	}
	if(a[i-1][j+1] == 1){
		a[i-1][j+1] = k;
		sima(i-1,j+1,k);
	}
	if(a[i][j-1] == 1){
		a[i][j-1] = k;
		sima(i,j-1,k);
	}
	if(a[i][j+1] == 1){
		a[i][j+1] = k;
		sima(i,j+1,k);
	}
	if(a[i+1][j-1] == 1){
		a[i+1][j-1] = k;
		sima(i+1,j-1,k);
	}
	if(a[i+1][j] == 1){
		a[i+1][j] = k;
		sima(i+1,j,k);
	}
	if(a[i+1][j+1] == 1){
		a[i+1][j+1] = k;
		sima(i+1,j+1,k);
	}
}

int main() {
	int w,h,k;
	while(1){
		for(int i = 0;i < 52;i++){
			for(int j = 0;j < 52;j++){
				a[i][j] = 0;
			}
		}
		cin >> w >> h;
		if(w == 0)break;
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				cin >> a[i][j];
			}
		}
		k = 2;
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				if(a[i][j] == 1){
					sima(i,j,k);
					k++;
				}
			}
		}
		cout << k-2 <<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1};
int dy[]={-1,-1,-1,0,0,1,1,1};

    int w,h;
void dfs(int i,int j,vector<vector<int>>& x){
        x[i][j]=0;
        for(int r=0;r<8;r++)
            if(0<=i+dx[r] && i+dx[r]<h && 0<=j+dy[r] && j+dy[r]<w && x[i+dx[r]][j+dy[r]])
                dfs(i+dx[r],j+dy[r],x);



}
int main(){
    while(cin>>w>>h,w){
        vector<vector<int>> x(h,vector<int>(w,0));
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                cin>>x[i][j];
        int cnt=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                if(x[i][j]){
                    cnt++;
                    dfs(i,j,x);


                }




            }
        cout<<cnt<<endl;



    }


    return 0;
}

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

using namespace std;
bool c[52][52]; 
int check(int arg[][52], int i, int j);

int main(void){
	int w, h;
	int arg[52][52];
	int i, j;
	int count;


	scanf("%d %d", &w, &h);
	while(w!=0 && h!=0){
		count=0;
		for(i=0; i<=h+1; i++){
			for(j=0; j<=w+1; j++){
				if(i==0 || i==(h+1) || j==0 || j==(w+1)){
					c[i][j]=false; 
					arg[i][j]=0;
				}
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){ 
				scanf("%d",&arg[i][j]);
				if(arg[i][j])
					c[i][j]=true;
				else
					c[i][j]=false;
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){
				if(arg[i][j]==1 && c[i][j]==true){
					count = count + check(arg, i, j);
				}
			}
		}

		printf("%d\n", count);
		scanf("%d %d", &w, &h);
	}

	return 0;
}

int check(int arg[][52], int i, int j){

	if(arg[i+1][j]==1 && c[i+1][j]==true){
		c[i+1][j]=false;
		check(arg, i+1, j);
	}
	if(arg[i][j+1]==1 && c[i][j+1]==true){
		c[i][j+1]=false;
		check(arg, i, j+1);
	}
	if(arg[i-1][j]==1 && c[i-1][j]==true){
		c[i-1][j]=false;
		check(arg, i-1, j);
	}
	if(arg[i][j-1]==1 && c[i][j-1]==true){
		c[i][j-1]=false;
		check(arg, i, j-1);
	}
	if(arg[i-1][j+1]==1 && c[i-1][j+1]==true){
		c[i-1][j+1]=false;
		check(arg, i-1, j+1);
	}
	if(arg[i-1][j-1]==1 && c[i-1][j-1]==true){
		c[i-1][j-1]=false;
		check(arg, i-1, j-1);
	}
	if(arg[i+1][j+1]==1 && c[i+1][j+1]==true){
		c[i+1][j+1]=false;
		check(arg, i+1, j+1);
	}
	if(arg[i+1][j-1]==1 && c[i+1][j-1]==true){
		c[i+1][j-1]=false;
		check(arg, i+1, j-1);
	}

	return 1;

}

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

void search(vector<vector<int>>& map, int x, int y)
{
	if (map[x][y] < 0)
		return;

	if (map[x][y] == 1) {
		map[x][y] = 0;
		search(map, x - 1, y);
		search(map, x + 1, y);
		search(map, x, y - 1);
		search(map, x, y + 1);
		search(map, x - 1, y - 1);
		search(map, x + 1, y + 1); 
		search(map, x + 1, y - 1);
		search(map, x - 1, y + 1);
	}
}
int main()
{
	int w, h;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		vector<vector<int>> map;
		map = vector<vector<int>>(h+2, vector<int>(w+2, -1));

		for (int i = 1; i < h+1; i++) {
			for (int j = 1; j < w+1; j++)
				cin >> map[i][j];
		}
		int island_n = 0;
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				if (map[i][j] == 1) {
					island_n++;
					search(map,i, j);
				}
			}
		}
		cout << island_n << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

int w,h;
int dim[52][52];
int mark[52][52];
int dirx[8] = {-1,-1,-1,0,0,1,1,1};
int diry[8] = {-1,0,1,-1,1,-1,0,1};
queue<pair<int,int> > q;
int check(int x,int y){
	if (x <0 || x >= h || y<0 || y>=w)return 0;
	return 1;
}
int main(){
	while(scanf("%d %d", &w, &h)!= EOF){
		if (w==0 && h==0)return 0;
		memset(mark, -1, sizeof(mark));
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				scanf("%d", &dim[i][j]);
			}	
		}
		int island = 0;
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				if (dim[i][j] == 1 && mark[i][j] == -1){
					island++;
					q.push(make_pair(i, j));
					mark[i][j] = 1;
					while(!q.empty()){
						pair<int, int> temp= q.front();
						q.pop();
						for (int k=0;k<8;k++){
							int tx = temp.first + dirx[k];
							int ty = temp.second + diry[k];
							if (check(tx,ty) == 1){
								if (dim[tx][ty] && mark[tx][ty] == -1){
									mark[tx][ty] = 1;
									q.push(make_pair(tx,ty));
								}
							}
						}
					}
				}
			}	
		}
		printf("%d\n", island);
	}
	return 0;
}