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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


bool c[64][64];
void destroy(int x, int y)
{
	if (!c[y][x])
		return;
	c[y][x] = false;
	for (int dy = -1; dy <= 1; ++dy)
		for (int dx = -1; dx <= 1; ++dx)
			destroy(x + dx, y + dy);
}
int main()
{
	int w, h;
	while (cin >> w >> h, w)
	{
		clear(c, 0);
		rep (y, h) rep (x, w)
			cin >> c[y + 1][x + 1];
		int res = 0;
		for (int y = 1; y <= h; ++y)
			for (int x = 1; x <= w; ++x)
				if (c[y][x])
					++res, destroy(x, y);
		cout << res << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int a[500][500];
void calc(int i,int j){
  if(a[i][j]==1){
    a[i][j]=2;
    calc(i+1,j);
    calc(i,j+1);
    calc(i+1,j+1);
    calc(i+1,j-1);
    calc(i-1,j);
    calc(i,j-1);
    calc(i-1,j-1);
    calc(i-1,j+1);
  }
  else return;
}

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

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

using namespace std;

using ll = long long;

int w, h;
vector< vector<int> > c, island;

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

void dfs(int x, int y, int id) {

    island[y][x] = id;

    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)
            continue;

        if(c[ny][nx] == 1 && island[ny][nx] != id)
            dfs(nx, ny, id);

    }

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int ans;

    while(cin >> w >> h && w) {

        c.assign(h, vector<int>(w));
        island.assign(h, vector<int>(w, -1));
        ans = 0;

        for(int y = 0; y < h; y++)
            for(int x = 0; x < w; x++)
                cin >> c[y][x];

        for(int x = 0; x < w; x++)
            for(int y = 0; y < h; y++)
                if(c[y][x] == 1 && island[y][x] == -1) {
                    dfs(x, y, ans);
                    ans++;
                }

        cout << ans << endl;

    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

bool done[60][60];
int islands[60][60];
void dfs(int vx, int vy, int w, int h)
{
    done[vx][vy] = true;
    int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1,};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1,};
    rep(i, 8)
    {
        if(islands[vx + dx[i]][vy + dy[i]] == 0) continue;
        if(vx + dx[i] < 0 || vx + dx[i] >= h) continue;
        if(vy + dy[i] < 0 || vy + dy[i] >= w) continue;
        if(done[vx + dx[i]][vy + dy[i]]) continue;
        dfs(vx + dx[i], vy + dy[i], w, h);
    }
    return;
}

int main(void)
{
    while(1)
    {
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        rep(i, h)rep(j, w) done[i][j] = false;
        rep(i, h)rep(j, w) cin >> islands[i][j];
        int ans = 0;
        rep(i, h)rep(j, w)
        {
            if(islands[i][j] == 0) continue;
            if(done[i][j]) continue;
            ans++;
            dfs(i, j, w, h);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int a[100][100]={};

void del(int x,int y){
  a[x][y]=0;
  for(int i=-1;i<=1;i++){
    for(int t=-1;t<=1;t++){
      if(a[x+i][y+t]==1){del(x+i,y+t);}
    }}
}

int main(){

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

  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      cin>>a[i][t];
    }}

  int count=0;
  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      if(a[i][t]==1){del(i,t); count++;}
    }}
  
  cout<<count<<endl;
  }
  
  return 0;
}