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>
using namespace std;
int mas[100][100];
int dx[8]={1,1,0,-1,-1,-1,0,1},dy[8]={0,1,1,1,0,-1,-1,-1};
int sx,sy,gx,gy,w,h,cnt=0;
void dfs(int x,int y);
int main(){
  while(1){
    cnt=0;
  cin >> w >> h;
  if(w==0 && h==0) break;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mas[i][j];
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mas[i][j]==1){
        cnt++;
        dfs(j,i);
      }
    }
  }
  cout << cnt << endl;
  }
}
void dfs(int x,int y){
  mas[y][x]=0;
  for(int i=0;i<8;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx >= 0&& nx < w && ny >= 0 && ny < h && mas[ny][nx]==1){
        dfs(nx,ny);
      }
  }
}

C++: #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
static const ll maxLL = (ll)1 << 62;

//dfs(bool型)
int dy[]={-1,0,1,0,1,-1,-1,1};
int dx[]={0,1,0,-1,1,1,-1,-1};
bool visited[1000][1000];
int t[1000][1000];
char copy_t[1000][1000];
int h,w;
int si,sj;

void dfs(int y,int x){
  if(y<=0||x<=0)return;
  if(y>h||x>w)return;
  if(t[y][x]==0)return;
  if(visited[y][x])return;
  visited[y][x]=true;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    dfs(ny,nx);
  }
  return;
}

int main(){
    int cnt=0;
    cin>>w>>h;
    repe(i,1,h){
        for(int j=1;j<=w;j++){
            cin>>t[i][j];
        }
    }
    repe(i,1,h){
        repe(j,1,w){
             if(t[i][j]==1 && visited[i][j]==false)cnt++;
        dfs(i,j);
        }
    }
    cout<<cnt<<endl;
    return 0;
}

C++: #include<cstdio>
using namespace std;
void func(int,int);
int a,b;
int sima[51][51];
int cnt;
int y,x;
int main(void)
{
	int i,j;
	scanf("%d",&a);
	scanf("%d",&b);
	while(!(a==0 && b==0)){
		
		for(i=0;i<b;i++){
			for(j=0;j<a;j++){
				scanf("%d",&sima[i][j]);
			}
		}
		cnt=0;
		for(i=0;i<b;i++){
			for(j=0;j<a;j++){
				if(sima[i][j]==1){
					func(i,j);
					cnt++;
				}
			}
		}
		printf("%d\n",cnt);
		scanf("%d",&a);
		scanf("%d",&b);
	}
	return 0;
}
void func(int y,int x)
{
	if(y-1>=0){
		if(sima[y-1][x]==1){
			sima[y-1][x]=0;
			func(y-1,x);
		}
	}
	if(x-1>=0){
		if(sima[y][x-1]==1){
			sima[y][x-1]=0;
			func(y,x-1);	
		}
	}
	if(y+1<=a){
		if(sima[y+1][x]==1){
			sima[y+1][x]=0;
			func(y+1,x);
		}
	}
	if(x+1<=b){
		if(sima[y][x+1]==1){
			sima[y][x+1]=0;
			func(y,x+1);
		}
	}
	if(y-1>=0 && x+1<=b){
		if(sima[y-1][x+1]==1){
			sima[y-1][x+1]=0;
			func(y-1,x+1);
		}
	}
	if(y-1>=0 && x-1>=0){
		if(sima[y-1][x-1]==1){
			sima[y-1][x-1]=0;
			func(y-1,x-1);
		}
	}
	if(y+1<=a && x+1<=b){
		if(sima[y+1][x+1]==1){
			sima[y+1][x+1]=0;
			func(y+1,x+1);
		}
	}
	if(y+1<=a && x-1>=0){
		if(sima[y+1][x-1]==1){
			sima[y+1][x-1]=0;
			func(y+1,x-1);
		}
	}
}

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

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

void init(){
  for(int i=0;i<55;i++)for(int j=0;j<55;j++)d[i][j]=0;
}

void Delete(int sx,int sy,int h,int w){
  queue<int>que;
  que.push(sx);que.push(sy);
  while(que.size()){
    int x=que.front();que.pop();
    int y=que.front();que.pop();
    for(int i=0;i<8;i++){
      if(d[y+dy[i]][x+dx[i]]==1&&0<=y+dy[i]&&y+dy[i]<h&&0<=x+dx[i]&&x+dx[i]<w){
	d[y+dy[i]][x+dx[i]]=0;
	que.push(x+dx[i]);
	que.push(y+dy[i]);
      }
    }
  }
}

int solve(int w,int h){
  int cnt=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(d[i][j]==1){
	Delete(j,i,h,w);
	cnt++;
      }
    }
  }
  return cnt;
}

int main(){
  int w,h;
  while(1){
    cin>>w>>h;
    if(w==0&&h==0)break;
    init();
    for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>d[i][j];
    cout<<solve(w,h)<<endl;
  }
}

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

using namespace std;

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

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

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 55) rep(j, 55) c[i + 1][j + 1] = 0;
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 55) rep(j, 55) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}