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 islands=0;
int w,h;
int field[50][50];
void bfs(int, int);

int main(){

  while(cin>>w>>h&&w!=0&&h!=0){

    for(int i=0;i<51;i++){
      for(int j=0;j<51;j++){
        field[i][j] = -1;
      }
    }

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        cin >> field[i][j];
        //        cout << field[i][j] << " ";
      }
      //    cout << endl;
    }
    // cout << endl;
    int ans=0; 

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        if(field[i][j]==1){
          field[i][j]=0;
          bfs(i,j);
          ans++;
        }
      }
    }
    /*   if(w==5&&h==4){
         for(int i=0;i<w;i++){
         for(int j=0;j<h;j++){
         cout << field[i][j] << " ";
         }
         cout << endl;
         }
         }*/
    cout << ans << endl;
  }

  return 0;
}

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

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

#define MAX 51

bool field[MAX][MAX];
int cnt;
int H, W;

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;
      int ny = y + dy;
      if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == 1) dfs(nx, ny);
    }
  }
}

int main(){
  while(cin >> W >> H){
    cnt = 0;
    if(W == 0 && H == 0) break;
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	cin >> field[i][j];
      }
    }

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

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

typedef pair<int, int> P;
int w,h;

void dfs(int x, int y, vector<vector<bool>> &maze){
    queue<P> q;
    q.push({x, y});
    maze[x][y] = false;
    while(q.size() > 0){
        int nx = q.front().first;
        int ny = q.front().second;
        q.pop();
        for (int i = -1; i <= 1; i++){
            for (int j = -1; j <= 1; j++){
                if (0 <= nx+i && nx+i < h && 0 <= ny+j && ny+j < w && maze[nx+i][ny+j]){
                    maze[nx+i][ny+j] = false;
                    q.push({nx+i, ny+j});
                }
            }
        }
    }
}

int main(){
    int x;
    int ans;
    while(1){
        cin >> w >> h;
        if (w == 0 || h == 0) break;
        vector<vector<bool>> maze(h, vector<bool>(w));
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin >> x;
                maze[i][j] = x;
            }
        }
        
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if (maze[i][j]){
                    dfs(i, j, maze);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>

using namespace std;


#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

int h,w;

int c[52][52];

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


void dfs(int i,int j);



int main(){


	while(1){
		cin >> w >> ;

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

		cnt=0;

		REP(i,h){
			REP(j,w){


				cin >> c[i][j] ;

			}
		}

		REP(i,h){
			REP(j,w){

				//cnt++??????????????\???????????????????????\???????????????
				if(c[i][j] == 1){
					cnt++;
					dfs(j,i);
				}
			}
		}

		cout << cnt << endl;
	}
	return 0;
}

void dfs(int i,int j){

	if(c[j][i] == 1){

		c[j][i] = 0;
		REP(k,8){
			//w???<=???????????£??????????????????
			if(0 <= i+dx[k] && i+dx[k] < w && 0 <= j+dy[k] && j+dy[k] < h){

				dfs(i+dx[k],j+dy[k]);
			}
		}
	}
}