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<cstdio>
#include<algorithm>
using namespace std;
int island[50][50];
void func(int,int);

int main(){
  int w,h;
  int ans;
  
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    ans=0;
    
    for(int i=0;i<h;i++){
      for(int l=0;l<w;l++){
        cin>>island[i][l];
      }
    }
    
    for(int i=0;i<h;i++){
      for(int l=0;l<w;l++){
        if(island[i][l]==1){
          func(i,l);
          ans++;
        }
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

void func(int x,int y){
  island[x][y]=0;
  for(int i=x-1;i<x+2;i++){
    for(int l=y-1;l<y+2;l++){
      if(island[i][l]==1)
        func(i,l);
    }
  }
}

C++: #include <iostream>

using namespace std;

int W,H;

bool table[50][50];


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 x,int y){
	table[x][y]=0;
	for(int i=0;i<8;i++){
		if(0<=x+dx[i]&&x+dx[i]<W&&0<=y+dy[i]&&y+dy[i]<H&&table[x+dx[i]][y+dy[i]])
			dfs(x+dx[i],y+dy[i]);
	}
	return ;
}


int main(void){
	while(cin >> W >> H,W|H){
		for(int y=0;y<H;y++)
			for(int x=0;x<W;x++)
				cin >> table[x][y];
		int num=0;
		for(int y=0;y<H;y++)
			for(int x=0;x<W;x++)
				if(table[x][y])
					num++,dfs(x,y);
		cout << num << 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;

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

	if(w==0||h==0) return 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",kazu);
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>

using namespace std;

int ans = 0;
bool dfs(vector< vector<int> > &island,int x,int y){
	int dx[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
	int dy[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
	if (island[x][y] == 0) return false;
	island[x][y] = 0;
	for (int i = 0; i< 8; i++){
		if(0 <= x + dx[i] and x + dx[i] < island.size() and 0 <= y + dy[i] and y + dy[i] < island[0].size()){
			dfs(island,x + dx[i],y + dy[i]);
		}
	}
	return true;
}

int main(){
	while(1){
		int w,h; cin >> w >> h;
		int ans = 0;
		if (w + h == 0) break;
		vector< vector<int> > island(h, vector<int>(w,0));
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> island[i][j];
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (dfs(island,i,j)){
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
vector<vector<int>> G;
void dfs(int a,int b){
     if(0>a|| a>=h || 0>b|| b>=w) return ;
     if( G.at(a).at(b)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(nx,ny);
        }
    }
}
 
 
 
int main(){
    cin>>w>>h;
    G.resize(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(i,j);
            }
        }
    }
cout<<count<<endl;
}