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 <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>
#define REP(i, n) for(LL i = 0;i < n;i++)
#define REPR(i, n) for(LL i = n;i >= 0;i--)
#define FOR(i, m, n) for(LL i = m;i < n;i++)
#define FORR(i, m, n) for(LL i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef long long LL;
//typedef LL int;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int w, h;
    while(cin >> w >> h, w + h){
        int c[51][51];
        REP(i, h){
            REP(j, w){
            cin >> c[i][j];
            }
        }

        int ans = 0;

        REP(i, h){
            REP(j, w){
                if(c[i][j] == 0) continue;
                else if(c[i][j] == 1){
                    queue<int> que;
                    que.push(i); que.push(j);
                    c[i][j] = 0;
                    while(!que.empty()){
                        int x, y;
                        y = que.front(); que.pop();
                        x = que.front(); que.pop();
                        
                        //??????
                        if(y-1 >= 0 && y-1 < h && x-1 >= 0 && x-1 < w && c[y-1][x-1] == 1){
                            que.push(y-1); que.push(x-1);
                            c[y-1][x-1] = 0;
                        }
                        //???
                        if(y-1 >= 0 && y-1 < h && x >= 0 && x < w && c[y-1][x] == 1){
                            que.push(y-1); que.push(x);
                            c[y-1][x] = 0;
                        }
                        //??????
                        if(y-1 >= 0 && y-1 < h && x+1 >= 0 && x+1 < w && c[y-1][x+1] == 1){
                            que.push(y-1); que.push(x+1);
                            c[y-1][x+1] = 0;
                        }
                        //???
                        if(y >= 0 && y < h && x-1 >= 0 && x-1 < w && c[y][x-1] == 1){
                            que.push(y); que.push(x-1);
                            c[y][x-1] = 0;
                        }
                        //???
                        if(y >= 0 && y < h && x+1 >= 0 && x+1 < w && c[y][x+1] == 1){
                            que.push(y); que.push(x+1);
                            c[y][x+1] = 0;
                        }
                        //??????
                        if(y+1 >= 0 && y+1 < h && x-1 >= 0 && x-1 < w && c[y+1][x-1] == 1){
                            que.push(y+1); que.push(x-1);
                            c[y+1][x-1] = 0;
                        }
                        //???
                        if(y+1 >= 0 && y+1 < h && x >= 0 && x < w && c[y+1][x] == 1){
                            que.push(y+1); que.push(x);
                            c[y+1][x] = 0;
                        }
                        //??????
                        if(y+1 >= 0 && y+1 < h && x+1 >= 0 && x+1 < w && c[y+1][x+1] == 1){
                            que.push(y+1); que.push(x+1);
                            c[y+1][x+1] = 0;
                        }
                    }
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

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

using namespace std;

typedef pair<int,int> P;

int c[1000][1000];//0??????
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
      cin>>w>>h;
      if(w==0&&h==0) break;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          cin>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include <iostream>

#define inRange(x, y, w, h) (0 <= x && x < w && 0 <= y && y < h)

using namespace std;

int w, h;
int fd[50][50];

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

void dfs( int x, int y )
{
	fd[y][x] = 0;
	for(int i=0; i < 8; ++i) {
		int mx = x + dx[i];
		int my = y + dy[i];
		if(inRange(mx, my, w, h) && fd[my][mx])
			dfs(mx, my);
	}
}

int main( void )
{
	while(cin >> w >> h, w | h) {
		for(int y=0; y < h; ++y)
			for(int x=0; x < w; ++x)
				cin >> fd[y][x];
		int count = 0;
		for(int y=0; y < h; ++y)
			for(int x=0; x < w; ++x)
				if(fd[y][x]) {
					++count;
					dfs(x, y);
				}
		cout << count << endl;
	}
	return 0;
}