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<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<tuple>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<iomanip>
#include<map>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF = 1e9 + 7;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int w, h;
int s[55][55];
void dfs(int y, int x) {

	s[y][x] = 0;

	for (int i = 0; i < 8; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];

		if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == 1) {
			dfs(ny, nx);
		}


	}


}



int main() {

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

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

		int cnt = 0;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (s[i][j] == 1) {

					dfs(i, j);
					cnt++;
				}


			}


		}


		cout << cnt << endl;

	}



	return 0;
}

C++: #include<iostream>
using namespace std;
int a[51][51]={},d[51][51]={},ans=0,w,h,dx[8]={-1,0,1,-1,1,-1,0,1},dy[8]={-1,-1,-1,0,0,1,1,1};
int f(int y,int x){
	if(d[y][x]==1 && a[y][x]==0){
		a[y][x]=1;
		for(int i=0;i<8;i++){
			if(y+dy[i]>-1 && y+dy[i]<h && x+dx[i]>-1 && x+dx[i]<w){
				if(a[y+dy[i]][x+dx[i]]==0)
				f(y+dy[i],x+dx[i]);
			}
		}
		return 1;
	}
	else{
		return 0;
	}
}
int main(){
	while(cin>>w>>h,w){
		ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				a[i][j]=0;
				cin>>d[i][j];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(f(i,j))
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

vector<vector<int> > d(MAX,vector<int>(MAX));
int w,h,data[MAX][MAX];

void dfs(int x, int y,int num){
    int vx[] = {1,-1,0,0,1,-1,1,-1};
    int vy[] = {0,0,1,-1,1,-1,-1,1};
    data[y][x] = num;

    for(int i=0; i<8; i++){
        int dx = vx[i] + x; int dy = vy[i] + y;
        if(dx >= w || dx < 0 || dy >= h || dy < 0 ||
            d[dy][dx] != 1 || data[dy][dx] == num) continue;
        dfs(dx,dy,num);
    }
}

int main(){
    while(1){
        cin>>w>>h;
        if(h == 0 && w == 0 ) break;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                 cin>>d[i][j];
            }
        }

        for(int i=0; i<MAX; i++){
            for(int j=0; j<MAX; j++){
                data[i][j] = -2;
            }
        }

        int ans = 0;

        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(d[i][j] == 1 && data[i][j] < 0){
                    dfs(j,i,ans);
                    ans++;
                }
            }
        }   
        cout<<ans<<endl;
    }
    return 0;
}

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

int w, h;
int map[100][100];
int island_number[100][100];

int angle[][2] = {
	{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
	{1, 0}, {1, 1}, {0, 1}, {-1, 1}
};

void dfs(int y, int x)
{
	for (int i = 0; i < 8; i++){
		if (
		y + angle[i][0] >= 0 &&
		y + angle[i][0] <= w &&
		x + angle[i][1] >= 0 &&
		x + angle[i][1] <= h &&
		island_number[y + angle[i][0]][x + angle[i][1]] == (y + angle[i][0]) * h + x + angle[i][1]
		){
			island_number[y + angle[i][0]][x + angle[i][1]] = island_number[y][x];
			dfs(y + angle[i][0], x + angle[i][1]);
		}
	}
}

int main()
{
	
	while (scanf("%d %d", &w, &h), w + h){
		memset(island_number, 0, sizeof(island_number));
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &map[i][j]);
				if (map[i][j] == 1){
					island_number[i][j] = i * h + j;
				}
				else {
					island_number[i][j] = -1;
				}
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] == i * h + j){
					dfs(i, j);
				}
			}
		}
		
		bool ex[100 * 100] = {false};
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] >= 0){
					ex[island_number[i][j]] = true;
				}
//				printf("%d ", island_number[i][j]);
			}
//			puts("");
		}
		
		int cnt = 0;
		for (int i = 0; i < w * h; i++){
			cnt += ex[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <iostream>

using namespace std;

int w,h;
int dx[8] = { -1 , -1 , -1 ,  0 , 0 ,  1 , 1 , 1};
int dy[8] = { -1 ,  0 ,  1 , -1 , 1 , -1 , 0 , 1};
int map[51][51];

void check(int x , int y){
	int mx,my;

	for(int i=0 ; i<8 ; i++){//üÍWûüð²×é
		mx = x + dx[i];
		my = y + dy[i];

		//tB[hOÉoÄ¢È¯êÎ²×é(0ÈãE³Èà)
		if(mx>=0 && mx<w && my>=0 && my<h){
			if(map[my][mx]==1){//¾Á½ç
				map[my][mx] = 2;//ñdÉ²×È¢½ßÉÊÌlðüêé
				check( mx , my );
			}
		}
	}
}

int main(){
	int count;//ÌðJEg·éÏ

	while(1){
		//ÏÌú»
		count = 0;
		
		//E³ÌüÍ
		cin >> w >> h;
		if(w==0 && h==0) break;//0 0ªüÍ³ê½çI¹

		//tB[hª©C©ÌîñðüÍ
		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				cin >> map[y][x];
			}
		}

		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				if(map[y][x]==1){//¾Á½çÄAÖðÄÑo·
					map[y][x] = 2;
					count++;
					check( x , y );
				}
			}
		}

		//¦ÌoÍ
		cout << count << endl;
	}

	return 0;
}