Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h, isl, v;
int a[2501];
deque<int> Q;
int main()
{
	while(cin >> w >> h && w>0)
	{
		for(int i=1; i<=w*h; i++)
		{
			cin >> a[i];
		}
		isl=0;
		for(int i=1; i<=w*h; i++)
		{
			if(a[i]==1)
			{
				isl++;
				Q.clear();
				Q.push_back(i);
				while(!Q.empty())
				{
					v=Q.front();
					Q.pop_front();
					if(v>w)
					{
						if(a[v-w]==1)
						{
							a[v-w]=0;
							Q.push_back(v-w);
						}
						if(v%w!=1)
						{
							if(a[v-w-1]==1)
							{
								a[v-w-1]=0;
								Q.push_back(v-w-1);
							}
						}
					}
					if(v%w!=1)
					{
						if(a[v-1]==1)
						{
							a[v-1]=0;
							Q.push_back(v-1);
						}
					}
					if(v%w!=0)
					{
						if(v>w)
						{
							if(a[v-w+1]==1)
							{
								a[v-w+1]=0;
								Q.push_back(v-w+1);
							}
						}
						if(a[v+1]==1)
						{
							a[v+1]=0;
							Q.push_back(v+1);
						}
						if(v+w<w*h)
						{
							if(a[v+w+1]==1)
							{
								a[v+w+1]=0;
								Q.push_back(v+w+1);
							}
						}
					}
					if(v+w<=w*h)
					{
						if(v%w!=1)
						{
							if(a[v+w-1]==1)
							{
								a[v+w-1]=0;
								Q.push_back(v+w-1);
							}
						}
						if(a[v+w]==1)
						{
							a[v+w]=0;
							Q.push_back(v+w);
						}
					}
				}
			}
		}
		cout << isl << endl;
	}
	return 0;
}

C++: #include <stdio.h>
int map[100][100],n,m;
bool went[100][100];
void walk(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return ;
    if(went[x][y])return ;
    if(map[x][y]==0)return ;
    went[x][y]=true;
    walk(x-1,y);
    walk(x+1,y);
    walk(x,y+1);
    walk(x,y-1);
    walk(x+1,y+1);
    walk(x+1,y-1);
    walk(x-1,y+1);
    walk(x-1,y-1);
    return ;
}
int main(){
    int ans;
    while(1){
        scanf("%d%d",&m,&n);
        if(n==0&&m==0)return 0;
        ans=0;
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            scanf("%d",&map[i][j]);
            went[i][j]=false;
        }
        for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(!went[i][j]&&map[i][j]==1){
            ans++;
            walk(i,j);
        }
        printf("%d\n",ans);
    }
}

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

using namespace std;


bool done[52][52];
int field[52][52];
int ofs[8][2] = {
  {0, 1},
  {1, 0},
  {-1, 0},
  {0, -1},
  {1, 1},
  {1, -1},
  {-1,-1},
  {-1, 1}
};

void paint(int y, int x)
{
  for (int i = 0; i < 8; ++i) {
    int nx = x + ofs[i][0];
    int ny = y + ofs[i][1];
    if (field[ny][nx] && !done[ny][nx]) {
      done[ny][nx] = true;
      paint(ny, nx);
    }
  }
}

int main() {
  int w, h;
  for(;;) {
    cin >> w >> h;
    if ((w|h) == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    memset(done, 0, sizeof(done));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> field[i][j];
      }
    }
    int cnt = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (field[i][j] && !done[i][j]) {
          paint(i, j);
          ++cnt;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}