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>
#include<string.h>
#include<utility>
#include<iostream>
#include<queue>

using namespace std;
typedef long long ll;
 
#define REP(i,n) for(int i=0;i<(int)(n);i++)
 
int main()
{
    int ans,w,h;
    int c[55][55];
    queue<pair<int,int> > que;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={-1,0,1,1,1,0,-1,-1};
    while(1==1)
    {
        ans=0;
        REP(i,55)REP(j,55)c[i][j]=10000;
        scanf("%d %d\n",&w,&h);
        if(w==0&&h==0)return 0;
        REP(i,h)REP(j,w)scanf("%d",&c[i+1][j+1]);
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(c[i][j]==1)
                {
                    ans++;
                    c[i][j]=0;
                    while(!que.empty())que.pop();
                    que.push(pair<int,int>(i,j));
                    pair<int,int> p;
                    while(!que.empty())
                    {
                        p=que.front();
                        que.pop();
                        REP(k,8)
                        {
                            if(c[p.first+dy[k]][p.second+dx[k]]==1)
                            {
                                que.push(pair<int,int>(p.first+dy[k],p.second+dx[k]));
                                c[p.first+dy[k]][p.second+dx[k]]=0;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y, int& a);

int main(){

	int w,h,ans;
	
	const int dif = 2;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			return 0;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 2;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(i, j, ans))
					ans++;

		std::cout << ans-dif << std::endl;
	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y, int& a){
	if(field[y][x] != 1)
		return false;

	field[y][x] = a;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j, a);

	return true;
}

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

using namespace std;

int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
int w, h, c[50][50];
void dfs(int c[50][50], int x, int y);

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)	break;
		int counter = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}

		for (int i = 0; i < w; i++) {
			for (int j = 0; j < h; j++) {
				if (c[i][j] == 1) {
					dfs(c, i, j);
					counter++;
				}
			}
		}
		cout << counter << endl;

	}
	return 0;
}

void dfs(int c[50][50], int x, int y) {	//(x, y) = 1ということを発見済み
	c[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) {
			if (c[x + dx[i]][y + dy[i]] == 1) {
				dfs(c, x + dx[i], y + dy[i]);
			}
		}
	}
}

C++: #include <iostream>

#define W_MAX 55
#define H_MAX 55

int f[H_MAX][W_MAX];

void dfs(int y,int x);


using namespace std;

int main(void)
{
	int w;
	int h;
	int i1,i2;
	int count;
	int a;

	while(1){
		cin >> w;
		cin >> h;
		if (w==0 && h==0) break;

		//cout << "a";

		for(i1=0;i1<H_MAX;i1++){
			for(i2=0;i2<W_MAX;i1++){
				f[i1][i2]=0;

			}
		}

		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				cin >> a;
				//f[i1][i2] = a;
			}
		}


		count=0;
		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				if (f[i1][i2] == 1){
					count++;
					dfs(i1,i2);
				}
			}
		}

		cout << count << endl;

	}
	return 0;
}


void dfs(int y,int x)
{
	int i1,i2;

	f[y][x]=0;
	for(i1=-1;i1<2;i1++){
		for(i2=-1;i2<2;i2++){

			if(f[y+i1][x+i2]==1) dfs(y+i1,x+i2);
		}
	}

	return;
}

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

int w, h, x, y, ans;
int mp[52][52];
int visited[52][52];
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1, 1, 1, -1, 0, 1, -1, 0};


void bfs(int x, int y){
  int i;
  visited[y][x] = 1;
  for(i = 0; i < 8; i++){
    if(visited[y + dy[i]][x + dx[i]] == 0 && mp[y + dy[i]][x + dx[i]] == 1){
      bfs(x + dx[i], y + dy[i]);
    }
  }
}

int main(void){
  while(1){
    cin >> w >> h;
    if(w == 0) break;

    for(y = 0; y < h + 2; y++){
      if(y == 0 || y == h + 1){
        for(x = 0; x < w + 2; x++) mp[y][x] = 0;
      }else{
        for(x = 0; x < w + 2; x++){
          if(x == 0 || x == w + 1){
            mp[y][x] = 0;
          }else{
            cin >> mp[y][x];
          }
        }
      }
    }
    for(y = 0; y < h + 2; y++){
      for(x = 0; x < w + 2; x++){
        visited[y][x] = 0;
      }
    }
    ans = 0;
    for(y = 1; y < h + 1; y++){
      for(x = 1; x < w + 1; x++){
        if(mp[y][x] == 1 && visited[y][x] == 0){
          ans++;
          bfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<vector<int> > imap(52,vector<int>(52,0));
int w,h;

void dfs(int nowx,int nowy){
  imap[nowx][nowy]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(imap[nowx+i][nowy+j]==1) dfs(nowx+i,nowy+j);
    }
  }
}

int main(){
  while(cin>> w>>h&&(w||h)){
    for(int i=1;i<h+1;++i)
      for(int j=1;j<w+1;++j)
	cin >> imap[i][j];

    int cnt=0;
    for(int i=1;i<h+1;++i)
      for(int j=1;j<w+1;++j)
	if(imap[i][j]==1){
	  cnt++;
	  dfs(i,j);
	}
    cout << cnt <<endl;
  }
  
  return 0;
}