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>
#include<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
    if(c[sx][sy]==false) return false;
    c[sx][sy]=false;
    for(int i=0;i<8;i++){
        int nx=sx+dx[i];
        int ny=sy+dy[i];
        bool flag=dfs(nx,ny,c);
    }
    return true;
}

int main(void){
    int h,w,temp;
    while(1){
        cin>>w>>h;
        if(w==0) break;
        vector<vector<bool> > c(h+2,vector<bool>(w+2,false)) ;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>temp;
                if(temp==1) c[i][j]=true;
            }
        }
        int t=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                bool flag=dfs(i,j,c);
                if(flag) t++;
            }
        }
        cout<<t<<endl;
    }
    return 0;
}

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;
	}
}

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

int land[50][50], flag[50][50];
int w, h;
int xroot[] = {0,0,1,1,1,-1,-1,-1};
int yroot[] = {1,-1,0,-1,1,0,1,-1};
int cnt = 1;


void dfs(int x, int y){


  if(land[y][x] != 1) return;
  land[y][x] = cnt;

  for(int i = 0; i < 8; i++){
    int nx = x + xroot[i], ny = y + yroot[i];
    if(ny >= h || nx >= w || ny < 0 || nx < 0) continue;
    // cout << nx << " " << ny << endl;
    dfs(nx,ny);
  }
}

int main(){

  while(cin >> w >> h && (w || h)){
    cnt = 1;
    memset(flag,0,sizeof(flag));

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



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

  }



  return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1e18;
long long mod=1e9+7;
int maze[55][55]={};
bool reached[55][55]={};
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
int w,h;
int islands;

void search(int nowy,int nowx,bool start){

    if(reached[nowy][nowx]||maze[nowy][nowx]==0)return;

    if(start)islands++;

    reached[nowy][nowx]=true;
    //cout<<nowy<<" "<<nowx<<" "<<endl;
    rep(i,8)
    {
        int nx=nowx+dx[i];
        int ny=nowy+dy[i];

        if(nx<1 || nx>w || ny<1 || ny>h||reached[ny][nx]|| !maze[ny][nx])continue;

        search(ny,nx,false);
    }
    //cout<<islands<<" end"<<endl;
    return;
}
int main(){
    

    while(1){
    
    cin>>w>>h;
    if(w==0 && h==0)break;
    //cout<<w<<" "<<h<<endl;
    islands=0;

    rep2(i,50)rep2(k,50){
        reached[i][k]=false;
        maze[i][k]=0;
    }
    rep2(i,h)rep2(k,w){
        int t;
        cin>>t;
        maze[i][k]=t;
    }

    rep2(i,h)rep2(k,w)search(i,k,true);

    cout<<islands<<endl;
    
    }
    return 0;

}
/*


*/