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<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<bitset>
using namespace std;
const int inf = 1001001000;

void dfs(vector<vector<int>> &graph, int v, set<int> &seen){
	seen.insert(v);
	for (int i=0;i<graph[v].size();i++){
		int nxt = graph[v][i];
		if (seen.count(nxt)) continue;
		dfs(graph, nxt, seen);
	}
}

void print(vector<vector<int>> a){
	for (int i=0;i<a.size(); i++){
		if (a[i].size() == 0) continue;
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

int main(){
	while(true){
		int w,h; cin >> w >> h;
		if (w == 0 && h == 0) break;
		vector<vector<int>> map(h+2,vector<int>(w+2,0));
		for (int i=1;i<h+1;i++){
			for (int j=1;j<w+1;j++){
				int c; cin >> c;
				map[i][j] = c;
			}
		}
		vector<vector<int>> graph;
		int r = h+2,c=w+2;
		for (int i = 0; i < r; i++){
			for (int j = 0; j < c;j++){
				vector<int> a;
				if (map[i][j] == 1){	
					for (int k = -1; k < 2; k++){
						for (int l = -1; l < 2; l++){
							if (map[i+k][j+l] == 1) a.push_back((i+k)*c+j+l);
						}
					}
					
				}
				graph.push_back(a);
			}
		}
		set<int> gseen;
		int ans = 0;
		for (int i = 0; i < graph.size(); i++){
			set<int> seen;
			if (graph[i].size() == 0) continue;
			if (gseen.find(i) == gseen.end()){
				dfs(graph,i,seen);
				ans++;
				set_union(seen.begin(),seen.end(),gseen.begin(),gseen.end(),inserter(gseen,gseen.end()));
			}
			
		}
		cout << ans << endl;
	}
	
	return 0;
}

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

#define INF INT_MAX
#define ll long long

using namespace std;

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

		//??????????????°????????????
		int ans = 0;
		for(int j = 0;j < h;j++){
			for(int i = 0;i < w;i++){
				if(c[j][i] == 1){
					ans++;
					vector< pair<int,int> > v;
					v.push_back(make_pair(j,i));
					while(!v.empty()){
						int y = v.back().first, x = v.back().second;
						c[y][x] = 0;	//????????¶????????????
						v.pop_back();
						for(int dirY = -1;dirY < 2;dirY++){
							for(int dirX = -1;dirX < 2;dirX++){
								int yy = y + dirY,xx = x + dirX;
								if((0 <= yy && yy < h) && (0 <= xx && xx < w) && (c[yy][xx] == 1)){
									v.push_back(make_pair(yy,xx));
								}
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

vvi t;
int W,H;

void dfs(int x,int y){
    t[x][y]='0';
    for(int i=0;i<8;i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx>=0 && nx<H && ny>=0 && ny<W && t[nx][ny]==1) dfs(nx,ny);
    }
    return;
}

int main(){
    while(cin>>W>>H){
        if(W==0&&H==0) return 0;

        t.resize(H);
        for(int i=0;i<H;i++) t[i].resize(W);

        for(int i=0;i<H;i++) for(int j=0;j<W;j++) cin>>t[i][j];

        int cnt=0;

        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(t[i][j]==1){
            cnt++;
            dfs(i,j);
        }
        cout << cnt << endl;
    }
}

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

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;

void sol(int x, int y)
{
  field[x][y] = 0;
  for (int i = 0; i < 8; i++){
    if ((0 <= x + dx[i] && x + dx[i] < h) || (0 <= y + dy[i] && y + dy[i] < w))
    if (field[x + dx[i]][y + dy[i]] == 1) sol(x + dx[i], y + dy[i]);
  }
}

int main()
{

  while (cin >> h >> w, h || w){
    int res = 0;
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> field[i][j];
      }
    }

    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        if (field[i][j] == 1){
          res++;
          sol(i, j);
        }
      }
    }
    cout << res << endl;
  }
}