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<bits/stdc++.h>
using namespace std;

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

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

	return;
}

int main(){

	int w, h;

	while (cin >> w >> h, w, h){

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

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

		cout << result << endl;

	}

	return 0;
}

//??????????????°??£????????????
//???????????±?????????????????????WA?????????
/*
int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

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

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

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					bool buff = island[c[j][i]];

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = buff && true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}
*/


/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  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);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < W ; i++) {
      for(int j = 0 ; j < H ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	  res++;
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

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

int field[51][51];
int w,h;

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

	for(int yy=-1;yy<=1;yy++){
		for(int xx=-1;xx<=1;xx++){
			if(x+xx>=0&&x+xx<=w&&y+yy>=0&&y+yy<=h){
			   if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	while(1){

	scanf("%d %d",&w,&h);

	if(w==0&&h==0) break;

	kazu=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

	printf("%d\n",kazu);
	}

	return 0;
}

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

int H, W;
vector<vector<int>> field(H, vector<int>(W));
vector<vector<bool>> seen(H, vector<bool>(W));

const int dh[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dw[8] = {0, 1, 0, -1, 1, -1, 1, -1};

void dfs(int h, int w)
{
    seen[h][w] = true;
    int nh, nw;
    for (int i=0; i<8; i++)
    {
        nh = h + dh[i];
        nw = w + dw[i];
        if (nh<0 || nh>=H || nw<0 || nw>=W) continue;
        if (seen[nh][nw]) continue;
        if (field[nh][nw]==0) continue;
        dfs(nh, nw);
    }
}

int main()
{
    while (cin >> W >> H)
    {
        if (W==0 || H==0) break;
        field.resize(H);
        seen.resize(H);
        for (int h=0; h<H; h++)
        {
            field[h].resize(W);
            seen[h].resize(W);
        }
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++) 
            {
                cin >> field[h][w];
                seen[h][w]=false;
            }
        }
        int answer=0;
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++)
            {
                if (seen[h][w]==false && field[h][w]==1) dfs(h, w), answer++;
            }
        }
        cout << answer << endl;
    }
}

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

int MAP(vector<vector<int>> &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int>> map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}