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<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,1,-1,-1};
int h,w;
int MAP[100][100];

int bfs(int y,int x){
  rep(i,8){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx>=0&&ny>=0&&nx<w&&ny<h&&MAP[ny][nx]==1){
      MAP[ny][nx]=0;
      bfs(ny,nx);
    }
  }
  return 1;
}

signed main(){
  while(cin>>w>>h,w){
    rep(i,h)rep(j,w)cin>>MAP[i][j];
    int ans=0;
    rep(i,h)rep(j,w){
      if(MAP[i][j]==1)ans+=bfs(i,j);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int field[52][52]={};
void umeru(int y,int x){
	field[y][x]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(field[y+i][x+j])	umeru(y+i,x+j);
		}
	}
}
int main(){
	while(1){
		int w,h,cnt=0;
		cin >> w >> h;
		if(!w&&!h)	break;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin >> field[i][j];
			}
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(field[i][j]){
					cnt++;
					umeru(i,j);
				}
			}
		}
		cout << cnt << 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;
}

C++: #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10000000
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
typedef vector<int> v;
typedef vector<int, int> v2;
typedef vector< pair<int, int> > vp;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int w, h;
int c[50][50];
int f[50][50];

bool dfs(int y, int x){
	if (c[y][x] == 0) return false;
	if (f[y][x] == 1) return false;
	
	f[y][x] = 1;
	if (y > 0) dfs(y - 1, x);
	if (x > 0) dfs(y, x - 1);
	
	if (y < h) dfs(y + 1, x);
	if (x < w) dfs(y, x + 1);

	if (y > 0 && x > 0) dfs(y - 1, x - 1);
	if (y < h && x > 0) dfs(y + 1, x - 1);
	if (y > 0 && x < w) dfs(y - 1, x + 1);
	if (y < h && x < w) dfs(y + 1, x + 1);

	return true;
}

int main(){
	ios::sync_with_stdio(false);

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

		int ans = 0;

		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> c[i][j];
			
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				f[i][j] = -1;
		
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if(dfs(i, j)) ans++;

		cout << ans << endl;

	}

	return 0;
}

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

int Count;
int w,h;
bool area[51][51]={};
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={1,1,0,-1,-1,-1,0,1};

void bfs(int x,int y){
  int X,Y;
  queue<pair<int,int> > Q;
  Q.push(make_pair(x,y));
  area[x][y]=0;
  while(!Q.empty()){
    pair<int,int> p1=Q.front();
    X=p1.first;
    Y=p1.second;
    Q.pop();
    for(int i=0;i<8;i++){
      if((X+dx[i]<=-1||Y+dy[i]<=-1)||(X+dx[i]>=w||Y+dy[i]>=h))continue;
      if(area[X+dx[i]][Y+dy[i]]==1){
        Q.push(make_pair(X+dx[i],Y+dy[i]));
        area[X+dx[i]][Y+dy[i]]=0;
      }
    }
  } 
  return;
}

int main(){
  int I;
  while(cin>>w>>h,w){
    Count=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>I;
        area[j][i]=I;
      }
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(area[j][i]==1){
          bfs(j,i);
          Count++;
        }
      }
    }
    cout<<Count<<endl;
  }
  return 0;
}