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, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int w, h;
int c[51][51];
bool f[51][51];
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
bool contain(int y, int x){
   return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    rep(i, 0, 8){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(!contain(ny, nx)) continue;
        if(f[ny][nx] || c[ny][nx] == 0) continue;
        dfs(ny, nx);
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin >> w >> h, w, h){
        rep(i, 0, h){
            rep(j, 0, w){
                cin >> c[i][j];
                f[i][j] = false;
            }
        }
        int ans = 0;
        rep(i, 0, h){
            rep(j, 0, w){
                if(c[i][j] == 1 && f[i][j] == false){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

// using namespace std;

using map_t = std::vector<std::vector<bool>>;

void fill(const map_t& map, int w, int h, int x, int y, map_t& visited) {
  if (x < 0 || w <= x || y < 0 || h <= y) {
    return;
  }
  if (!map[y][x] || visited[y][x]) {
    return;
  }
  visited[y][x] = true;
  fill(map, w, h, x - 1, y - 1, visited);
  fill(map, w, h, x + 0, y - 1, visited);
  fill(map, w, h, x + 1, y - 1, visited);
  fill(map, w, h, x - 1, y + 0, visited);
  fill(map, w, h, x + 1, y + 0, visited);
  fill(map, w, h, x - 1, y + 1, visited);
  fill(map, w, h, x + 0, y + 1, visited);
  fill(map, w, h, x + 1, y + 1, visited);
}

int solve(const map_t& map, int w, int h) {
  map_t visited(h, std::vector<bool>(w, false));
  int ret = 0;
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; x++) {
      if (map[y][x] && !visited[y][x]) {
        ret++;
        fill(map, w, h, x, y, visited);
      }
    }
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  while (true) {
    int w, h;
    std::cin >> w >> h;
    if (w == 0 || h == 0) {
      break;
    }
    map_t map(h, std::vector<bool>(w));
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; x++) {
        int a;
        std::cin >> a;
        map[y][x] = a != 0;
      }
    }
    std::cout << solve(map, w, h) << std::endl;
  }
  return 0;
}

C++: #include<cstdio>
int n, m,ans;
int a[100][100];
bool b[100][100];

void dfs(int x,int y) {
	b[x][y] = true;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (0 <= x + i&&x + i < n) {
				if (0 <= y + j&&y + j < m) {
					if (a[x+i][y+j] == 1 && !b[x+i][y+j])dfs(x+i, y+j);
				}
			}
		}
	}
	return;
}

int main(void) {

	while (true) {
		scanf("%d %d", &m, &n);
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) {
				a[i][j] = 0;
				b[i][j] = false;
			}
		}
		for (int i = 0; i < n;i++) {
			for (int j = 0; j < m; j ++) {
				scanf("%d", &a[i][j]);
			}
		}
		ans = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (a[i][j] == 1 && !b[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int w,h;
int field[51][51];
void dfs(int x,int y);

int main(){
	while(cin>>w>>h&&w!=0&&h!=0){
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				cin>>field[y][x];
			}
		}
		int count=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(field[y][x]==1){
					dfs(x,y);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}

}

void dfs(int x,int y){
	field[y][x]=-1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int mx=x+i;
			int my=y+j;

			if(mx<0||my<0||mx>=w||my>=h)continue;
			if(field[my][mx]==1){
				dfs(mx,my);
			}
		}
	}
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+2, y);
    dfs(x+2, y+1);
    dfs(x+2, y-1);
    dfs(x-2, y);
    dfs(x-2, y+1);
    dfs(x-2, y-1);
    dfs(x, y+1);
    dfs(x, y-1);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            scanf("%s\n", c[k]);
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}