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 <stack>
#include <cstdio>

using namespace std;
stack<int> y;
stack<int> x;
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, si, sj;
	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){
					y.push(i);
					x.push(j);
					while(!x.empty() && !y.empty()){
							si=y.top();sj=x.top();
							y.pop();x.pop();
							check(arg, si, sj);

					}
					count++;

				}
			}
		}


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

	return 0;
}

int check(int arg[][52], int i, int j){
	int flag = 0;

	c[i][j]=false;
	if(arg[i+1][j]==1 && c[i+1][j]==true){
		c[i+1][j]=false;
		y.push(i+1);
		x.push(j);
		flag = 1;
	}
	if(arg[i][j+1]==1 && c[i][j+1]==true){
		c[i][j+1]=false;
		y.push(i);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i-1][j]==1 && c[i-1][j]==true){
		c[i-1][j]=false;
		y.push(i-1);
		x.push(j);
		flag = 1;
	}
	if(arg[i][j-1]==1 && c[i][j-1]==true){
		c[i][j-1]=false;
		y.push(i);
		x.push(j-1);
		flag = 1;
	}
	if(arg[i-1][j+1]==1 && c[i-1][j+1]==true){
		c[i-1][j+1]=false;
		y.push(i-1);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i-1][j-1]==1 && c[i-1][j-1]==true){
		c[i-1][j-1]=false;
		y.push(i-1);
		x.push(j-1);
		flag = 1;
	}
	if(arg[i+1][j+1]==1 && c[i+1][j+1]==true){
		c[i+1][j+1]=false;
		y.push(i+1);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i+1][j-1]==1 && c[i+1][j-1]==true){
		c[i+1][j-1]=false;
		y.push(i+1);
		x.push(j-1);
		flag = 1;
	}

	if(flag)
		return 1;
	else
		return 0;
}

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

// 入力
int H, W;
vector<vector<int>> field;

// 探索
void dfs(int h, int w) {
    field[h][w] = 0;

    // 八方向を探索
    for (int dh = -1; dh <= 1; ++dh) {
        for (int dw = -1; dw <= 1; ++dw) {
            int nh = h + dh, nw = w + dw;

            // 場外アウトしたり、0 だったりはスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == 0) continue;

            // 再帰的に探索
            dfs(nh, nw);
        }
    }
}

int main() {
    // 入力受け取り
    while (cin >> W >> H) {
        if (H == 0 || W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; ++h) for (int w = 0; w < W; ++w) cin >> field[h][w];

        // 探索開始
        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (field[h][w] == 0) continue; // 残島じゃなかったらスルー
                dfs(h, w);
                ++count;
            }
        }
        cout << count << endl;
    }
}

C++: #include <stdio.h>
#include <string.h>

void island_delete(int data[][60], int w, int h, int self_i, int self_j) {
  data[self_i][self_j] = 0;
  if (self_j-1 >= 0 && data[self_i][self_j-1] == 1) {
	island_delete(data,w,h,self_i,self_j-1);
  }
  if (self_i-1 >= 0 && data[self_i-1][self_j] == 1) {
	island_delete(data,w,h,self_i-1,self_j);
  }
  if (self_j+1 <= w && data[self_i][self_j+1] == 1) {
	island_delete(data,w,h,self_i,self_j+1);
  }
  if (self_i+1 <= h && data[self_i+1][self_j] == 1) {
	island_delete(data,w,h,self_i+1,self_j);
  }

  if (self_j-1 >= 0 && self_i-1 >= 0 && data[self_i-1][self_j-1] == 1) {
	island_delete(data,w,h,self_i-1,self_j-1);
  }
  if (self_j+1 <= w && self_i-1 >= 0 && data[self_i-1][self_j+1] == 1) {
	island_delete(data,w,h,self_i-1,self_j+1);
  }
  if (self_j-1 >= 0 && self_i+1 <= h && data[self_i+1][self_j-1] == 1) {
	island_delete(data,w,h,self_i+1,self_j-1);
  }
  if (self_j+1 <= w && self_i+1 <= h && data[self_i+1][self_j+1] == 1) {
	island_delete(data,w,h,self_i+1,self_j+1);
  }


  
}

int main(void) {
  while(1) {
	int w, h;
	int cnt = 0;
	int data[60][60] = {{0}};
	scanf("%d%d", &w, &h);
	if (w == h && w == 0) break;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		scanf("%d", &data[i][j]);
	  }
	}
	int self_i, self_j;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		if (data[i][j] == 1) {
		  self_i = i; self_j = j; cnt++;
		  island_delete(data,w,h,self_i,self_j);
		}
	  }
	}
	printf("%d\n",cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

void dfs(vector< vector<int> > &island, int x, int y) {
    island[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 (nx >= 0 && nx < island.size() && ny >= 0 && ny < island[0].size() && island[nx][ny] == 1) {
                dfs(island, nx, ny);
            }
        }
    }
}

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

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

        int res = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (island[i][j] == 1) {
                    dfs(island, i, j);
                    res++;
                }
            }
        }

        cout << res << endl;
    }

    return 0;
}