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>
using namespace std;
int p[53][53];

class point{
public:
	int x, y;
	point(int newx, int newy){
		x = newx;
		y = newy;
	}
};

void findisland(int i, int j){
	p[i][j] = 2;
	if (p[i - 1][j - 1] == 1)
		findisland(i - 1, j - 1);
	if (p[i][j - 1] == 1)
		findisland(i, j - 1);
	if (p[i + 1][j - 1] == 1)
		findisland(i + 1, j - 1);
	if (p[i - 1][j] == 1)
		findisland(i - 1, j);
	if (p[i + 1][j] == 1)
		findisland(i + 1, j);
	if (p[i - 1][j + 1] == 1)
		findisland(i - 1, j + 1);
	if (p[i][j + 1] == 1)
		findisland(i, j + 1);
	if (p[i + 1][j + 1] == 1)
		findisland(i + 1, j + 1);
}


int main()
{
	int w, h, count;
	while (cin >> w >> h, (w | h)){

		//初期化
		for (int i = 0; i < 53; i++){
			for (int j = 0; j < 53; j++){
				p[i][j] = 0;
			}
		}
		count = 0;

		//入力
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				cin >> p[i][j];
			}
		}

		//探索
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				if (p[i][j] == 1){
					findisland(i, j);
					count++;
				}
			}
		}
		cout << count << endl;



	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
int field[50][50];

void input(void){
	cin >> W;
	cin >> H;

	for(int i = 0;i < H;i++){
		for(int j = 0;j < W;j++){
			cin >> field[i][j];
		}
	}
}

void dfs(int x, int y){
	field[x][y] = 0;

	for(int dx = -1;dx <= 1;dx++){
		for(int dy = -1;dy <= 1;dy++){
			int nx = x + dx, ny = y + dy;
			if(0 <= nx && nx <= W && 0 <= ny && ny <= H && field[nx][ny] == 1) dfs(nx, ny);
		}
	}

	return ;
}

int main(){
	while(1){
		int res = 0;
		input();
		if(W == 0 && H == 0) break;

		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(field[i][j] == 1){
					dfs(i, j);
					res++;
				}
			}
		}

		cout << res << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
int w, h, cnt;
int c[51][51];
void dfs(int i, int j){
    if(i-1 >= 0 && j-1 >= 0){
        if(c[i-1][j-1] == 1){
            c[i-1][j-1] = 0;
            dfs(i-1,j-1);
        }
    }
    if(i-1 >= 0){
        if(c[i-1][j] == 1){
            c[i-1][j] = 0;
            dfs(i-1,j);
        }
    }
    if(i-1 >= 0 && j+1 < w){
        if(c[i-1][j+1] == 1){
            c[i-1][j+1] = 0;
            dfs(i-1,j+1);
        }
    }
    if(j-1 >= 0){
        if(c[i][j-1] == 1){
            c[i][j-1] = 0;
            dfs(i,j-1);
        }
    }
    if(j+1 < w){
        if(c[i][j+1] == 1){
            c[i][j+1] = 0;
            dfs(i,j+1);
        }
    }
    if(i+1 < h && j-1 >= 0){
        if(c[i+1][j-1] == 1){
            c[i+1][j-1] = 0;
            dfs(i+1,j-1);
        }
    }
    if(i+1 < h){
        if(c[i+1][j] == 1){
            c[i+1][j] = 0;
            dfs(i+1,j);
        }
    }
    if(i+1 < h && j+1 < w){
        if(c[i+1][j+1] == 1){
            c[i+1][j+1] = 0;
            dfs(i+1,j+1);
        }
    }

}

int main(void){
    while(1){
        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];
            }
        }

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

C++: #include <iostream>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x,st_y;
		std::stack<int> st_xn,st_yn;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(w+2, std::vector<bool>(h+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);

				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}
		return 0;
}