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<stdio.h>
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]);
			}
		}
		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++;
					draw(j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}
void draw(int x,int 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);
			}
		}
	}
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
void dfs(vector<vector<int>> &c,int nx,int ny)
{
  int i,j;
  c[nx][ny]=0;
  for(i=-1;i<=1;i++)for(j=-1;j<=1;j++)
  {
    if(i==0&&j==0)continue;
    if(c[nx+i][ny+j]==1)
    {
      dfs(c,nx+i,ny+j);
    }
  }
}

int main()
{
  int w,h;cin>>w>>h;
  while(w!=0||h!=0)
  {
    int i,j;
    vector<vector<int>> c(h+2,vector<int>(w+2,0));
    rep(i,h)rep(j,w)cin>>c[i+1][j+1];
    int ans=0;
    rep(i,h)rep(j,w)
    {
      if(c[i+1][j+1]==1)
      {
        dfs(c,i+1,j+1);
        ans++;
      }
    }
    cout<<ans<<endl;
    cin>>w>>h;
  }
}

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

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int w,h;
    while (cin>>w>>h) {
        if (w==0&&h==0) {
            break;
        }
        char c;
        std::vector<vector<char>> vec(h,vector<char>(w));
        bool seen[h][w];
        for (int i=0;i<h;i++) {
            for (int j=0;j<w;j++) {
                cin>>c;
                vec[i][j]=c;
                seen[i][j]=false;
            }
        }
        int count=0;
        for (int i=0;i<h;i++) {
            for (int j=0;j<w;j++) {
                if (vec[i][j]=='1'&&seen[i][j]==false) {
                    seen[i][j]=true;
                    std::stack<pair<int,int>> stk;
                    stk.push(make_pair(i,j));
                    while (stk.size()!=0) {
                        int x=stk.top().first;
                        int y=stk.top().second;
                        if (x+1<h&&seen[x+1][y]==false&&vec[x+1][y]=='1') { //下
                            stk.push(make_pair(x+1,y));
                            seen[x+1][y]=true;
                        } else if (y+1<w&&seen[x][y+1]==false&&vec[x][y+1]=='1') { //右
                            stk.push(make_pair(x,y+1));
                            seen[x][y+1]=true;
                        } else if (x+1<h&&y-1>=0&&seen[x+1][y-1]==false&&vec[x+1][y-1]=='1') { //左下
                            stk.push(make_pair(x+1,y-1));
                            seen[x+1][y-1]=true;
                        } else if (x+1<h&&y+1<w&&seen[x+1][y+1]==false&&vec[x+1][y+1]=='1') { //右下
                            stk.push(make_pair(x+1,y+1));
                            seen[x+1][y+1]=true;
                        } else if (x-1>=0&&y-1>=0&&seen[x-1][y-1]==false&&vec[x-1][y-1]=='1') { //左上
                            stk.push(make_pair(x-1,y-1));
                            seen[x-1][y-1]=true;
                        } else if (x-1>=0&&y+1<w&&seen[x-1][y+1]==false&&vec[x-1][y+1]=='1') { //右上
                            stk.push(make_pair(x-1,y+1));
                            seen[x-1][y+1]=true;
                        } else if (x-1>=0&&seen[x-1][y]==false&&vec[x-1][y]=='1') { //上
                            stk.push(make_pair(x-1,y));
                            seen[x-1][y]=true;
                        } else if (y-1>=0&&seen[x][y-1]==false&&vec[x][y-1]=='1') { //左
                            stk.push(make_pair(x,y-1));
                            seen[x][y-1]=true;
                        } else {
                            stk.pop();
                        }
                    }
                    count++;
                }
            }
        }
        cout<<count<<endl;
    }
}