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;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  while(true){
    int W,H;
    cin>>W>>H;
    if(H==0&&W==0)break;
    vector<vector<int>>c(H,vector<int>(W));
    for(int i=0;i<H;i++)for(int j=0;j<W;j++)cin>>c[i][j];
    queue<pii>q;
    int dx[8]={0,0,1,-1,1,-1,-1,1};
    int dy[8]={1,-1,0,0,1,-1,1,-1};
    int ans=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(c[i][j]==0)continue;
        c[i][j]=0;
        q.push({i,j});
        while(q.size()){
          int y=q.front().first;
          int x=q.front().second;
          q.pop();
          for(int k=0;k<8;k++){
            int X=x+dx[k];
            int Y=y+dy[k];
            if(0<=X&&X<W&&0<=Y&&Y<H&&c[Y][X]==1){
              c[Y][X]=0;
              q.push({Y,X});
            }
          }
        }
        ans++;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

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

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

void oneIsland(int x, int y) {
	map[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (nextX < 0 || w <= nextX) continue;
		if (nextY < 0 || w <= nextY) continue;
		if (map[nextY][nextX] == 1) {
			oneIsland(nextX, nextY);
		}
	}
}

int main() {
	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 >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					oneIsland(j, i);
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}

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

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

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

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if ((0 <= nextX && nextX < w)
			&& (0 <= nextY && nextY < h)) {
			if (map[nextY][nextX] == 1) {
				map[nextY][nextX] = 0;
				oneIsland(nextX, nextY);
			}
		}
	}
}

int main() {
	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 >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					count++;
					oneIsland(j, i);
				}
			}
		}
		cout << count <<endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int c[50][50],w,h;
int dx[]={-1,-1,-1,0,0,1,1,1};
int dy[]={-1,0,1,-1,1,-1,0,1};
void dfs(int y,int x){
  if(c[y][x]){
    c[y][x]=0;
    int i;
    for(i=0;i<8;i++){
      dfs(y+dy[i],x+dx[i]);
    }
  }
  return;
}

int main(){
  int i,j,ans;
  cin >> w >> h;
  while(1){
    ans=0;
    if(w+h==0)break;
    for(i=0;i<50;i++)for(j=0;j<50;j++)c[i][j]=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)cin >> c[i][j];
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)if(c[i][j]){
        dfs(i,j);
        ans++;
      }
    }
    cout << ans << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
vvi v(60,vi(60,0));
int w,h;
int mx[]={-1,0,1};
int my[]={-1,0,1};
void dfs(int y,int x){
        if (!v[y][x])return;
        if(v[y][x]){
            v[y][x]=0;
            rep(p,3)rep(q,3){
                int tx=x+mx[p]; int ty=y+my[q];
                if(0<=tx&&tx<w&&0<=ty&&ty<h&&v[ty][tx]!=0){
                    dfs(ty,tx);
                }
            }
        }
    }
int main(){
    while(cin>>w>>h,w+h){
        rep(i,h)rep(j,w)cin>>v[i][j];
        int ans=0;
        rep(i,h)rep(j,w){
            if(v[i][j]){dfs(i,j);
            ans++;
            }
        }
        cout<<ans<<endl;
        rep(i,60)rep(j,60)v[i][j]=0;
    }
}

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

int w, h;
int data[99][99]; //y,x

int solve(int x, int y){

  if(x < 0 || y < 0 || x == w || y == h) return 0;
  if(!data[y][x]) return 0;

  data[y][x] = 0;
  for(int i=-1;i<2;i++) for(int j=-1;j<2;j++) solve(x+j, y+i);

  return 1;
}

int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

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

    int ret = 0;
    for(int i=0;i<h;i++) for(int j=0;j<w;j++) ret += solve(j, i);

    cout << ret << endl;
  }

}