Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<vector<int>> v;

int w,h;
void dfs(int y, int x, vector<vector<bool>> &used){
    for(int dy=-1;dy<=1;dy++){
        for(int dx=-1;dx<=1;dx++){
            if(dy==0 && dx==0)continue;
            int ddy = y+dy, ddx = x+dx;
            if(ddx<0 || ddy<0 || ddx>=w || ddy>=h)continue;
            if(v[ddy][ddx]!=1 || used[ddy][ddx]==true)continue;
            used[ddy][ddx] = true;
            dfs(ddy,ddx,used);
        }
    }
}

int main(){
    while(cin>>w>>h&&(w||h)){
        
        v.clear();
        v.resize(h,vector<int>(w));
        rep(i,h)rep(j,w)cin>>v[i][j];
        
        int ans = 0;
        vector<vector<bool>> used(h,vector<bool>(w,false));
        rep(i,h){
            rep(j,w){
                if(used[i][j]==false && v[i][j]==1){
                    used[i][j] = true;
                    dfs(i,j,used);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}

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

constexpr int MAX_H = 55;
constexpr int MAX_W = 55;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

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

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

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

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

int myMap[50][50];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < w && y < h);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

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

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

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1) {
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
    
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;

#define INF 999999999

int map[52][52];
bool ch[52][52];
int ans;

void dfs(int map[52][52], bool c[52][52], int x, int y){
	//cout << x << " " << y << " " << ans << endl;
	c[x][y] = 1;
	if(map[x-1][y]==1 && c[x-1][y]==0) dfs(map,c,x-1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x+1][y]==1 && c[x+1][y]==0) dfs(map,c,x+1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x][y-1]==1 && c[x][y-1]==0) dfs(map,c,x,y-1);
	if(map[x][y+1]==1 && c[x][y+1]==0) dfs(map,c,x,y+1);
}

int main(){
	int i, j, w, h;
	while(1){
		cin >> w >> h;
		if(!w) break;
		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)map[i][j]=0;
				else cin >> map[i][j];
				ch[i][j] = 0;
			}
		}
		ans = 0;
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				if(map[i][j]&&!ch[i][j]){
					dfs(map,ch,i,j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int h,w;
int G[1000][1000];
int dx[] = {-1,0,1,-1,1,-1,0,1},dy[] = {-1,-1,-1,0,0,1,1,1};

void dfs(int px,int py){
  G[px][py] = 0;
  for(int i = 0 ; i < 8 ; i++ ){
    int nx = px + dx[i],ny = py + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && G[nx][ny] == 1){
      dfs(nx,ny);
    }
  }
}

int main(){
  while( cin >> w >> h,w+h){

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

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