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>

#define rep(i, n) for(int i = 0 ; i < n ; i++)

using namespace std;

int area[50][50], cnt = 0;
int w, h;

void dfs(int x ,int y)
{
  area[x][y] = 0;

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1){
	dfs(nx, ny);
      }
    }
  }
}

int main()
{
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    cout << cnt << '\n';
    cnt = 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;
void dfs(vector<vector<int>> &G,int a,int b){
     if(0>nx|| nx>=h || 0>ny|| ny>=w) return ;
     if( G.at(nx).at(ny)==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(G,nx,ny);
        }
    }
}
 
 
 
int main(){
    cin>>w>>h;
    vector<vector<int>>G(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(G,i,j);
            }
        }
    }
cout<<count<<endl;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[51][51];

int main(){
	int i,j;
	int count;


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


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(long long int i = 0; i < n; i++)
#define _rep(i,m,n) for(long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int H,W;
vector<vector<int>> field;

void dfs(int h, int w){
   field[h][w] = 0;
   
   for(int dh = -1; dh <= 1; dh++){
      for(int dw = -1; dw <= 1; dw++){
         int nh = h + dh;
         int nw = w + dw;
      
         if(nh < 0 || nh >= H || nw < 0 || nw >= W)continue;
         if(field[nh][nw] == 0)continue;

         dfs(nh, nw);
      }
   }
}

int main(){
   while(true){
   cin >> W >> H;
   if(H == 0 || W == 0)return 0;
   field.assign(H, vector<int>(W, 0));

   for (int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
         cin >> field[i][j];
      }
   }

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

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

int w, h, c, n;
int a[50][50];
queue<int> q;

void bfs() {
	int x, y;
	while (1) {
		x = q.front() % w;
		y = q.front() / w;
		
		if ( x > 0 && y > 0 && a[y - 1][x - 1] == 1 ) {
			a[y - 1][x - 1] = 0;
			q.push( ( y - 1 ) * w + x - 1 );
		}
		if ( x < w - 1 && y > 0 && a[y - 1][x + 1] == 1 ) {
			a[y - 1][x + 1] = 0;
			q.push( ( y - 1 ) * w + x + 1 );
		}
		if ( x > 0 && y < h - 1 && a[y + 1][x - 1] == 1 ) {
			a[y + 1][x - 1] = 0;
			q.push( ( y + 1 ) * w + x - 1 );
		}
		if ( x < w - 1 && y < h - 1 && a[y + 1][x + 1] == 1 ) {
			a[y + 1][x + 1] = 0;
			q.push( ( y + 1 ) * w + x + 1 );
		}
		if ( x > 0 && a[y][x - 1] == 1 ) {
			a[y][x - 1] = 0;
			q.push( y * w + x - 1 );
		}
		if ( y > 0 && a[y - 1][x] == 1 ) {
			a[y - 1][x] = 0;
			q.push( ( y - 1 ) * w + x );
		}
		if ( x < w - 1 && a[y][x + 1] == 1 ) {
			a[y][x + 1] = 0;
				q.push( y * w + x + 1 );
		}
		if ( y < h - 1 && a[y + 1][x] == 1 ) {
			a[y + 1][x] = 0;
			q.push( ( y + 1 ) * w + x );
		}
		q.pop();
		if ( q.empty() ) break;	
	}
	n++;
}

int main() {

	while (1) {
		n = 0;
		for ( int j = 0; j < 50; j++ )
			for ( int i = 0; i < 50; i++ )
				a[j][i] = 0;
		cin >> w >> h;
		if ( w == 0 ) break;
		for ( int j = 0; j < h; j++ ) {
			for ( int i = 0; i < w; i++ ) {
				cin >> c;
				a[j][i] = c;
			}
		}
		for ( int j = 0; j < h; j++ ) {
			for ( int i = 0; i < w; i++ ) {
				if ( a[j][i] == 1 ) {
					a[j][i] = 0;
					q.push( j * w + i );
					bfs();
				}
			}
		}
		cout << n << endl;
	}
}