Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: import sys

sys.setrecursionlimit(10**5)

def solve():
    while 1:
        w, h = map(int, sys.stdin.readline().split())

        if w == h == 0:
            return

        room = [[int(j) for j in sys.stdin.readline().split()] for i in range(h)]

        cnt = 0

        for i in range(h):
            for j in range(w):
                if room[i][j]:
                    cnt += 1
                    dfs(w, h, room, i, j)

        print(cnt)

def dfs(w, h, room, i, j):
    if i < 0 or i >= h or j < 0 or j >= w or (not room[i][j]):
        return

    room[i][j] = 0

    dfs(w, h, room, i + 1, j)
    dfs(w, h, room, i - 1, j)
    dfs(w, h, room, i, j + 1)
    dfs(w, h, room, i, j - 1)
    dfs(w, h, room, i + 1, j + 1)
    dfs(w, h, room, i + 1, j - 1)
    dfs(w, h, room, i - 1, j + 1)
    dfs(w, h, room, i - 1, j - 1)

if __name__ == '__main__':
    solve()

C++: #include <bits/stdc++.h>
using namespace std;
int ans = 1e9;
int H,W;
int mp[50][50];
int nx,ny;
void func(int y,int x){
	//cout << y << ' ' <<x<<endl;
	if(mp[y][x] == 1){
		mp[y][x] = 2;
		func(y,x);
	}
	if(mp[y+1][x] == 1){
		mp[y+1][x] = 2;
		func(y+1,x);
	}
	if(mp[y][x+1] == 1){
		mp[y][x+1] = 2;
		func(y,x+1);
	}
	if(mp[y+1][x+1] == 1){
		mp[y+1][x+1] = 2;
		func(y+1,x+1);
	}
	if(mp[y-1][x] == 1){
		mp[y-1][x] = 2;
		func(y-1,x);
	}
	if(mp[y][x-1] == 1){
		mp[y][x-1] = 2;
		func(y,x-1);
	}
	if(mp[y-1][x-1] == 1){
		mp[y-1][x-1] = 2;
		func(y-1,x-1);
	}
	if(mp[y+1][x-1] == 1){
		mp[y+1][x-1] = 2;
		func(y+1,x-1);
	}
	if(mp[y-1][x+1] == 1){
		mp[y-1][x+1] = 2;
		func(y-1,x+1);
	}
}
void findL(){
	nx = -1;ny = -1;
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(mp[i][j] == 1){
				nx = j,ny = i;
				//cout <<nx<<ny<<endl;
				i = H; j = W;
			}
		}
	}
}
int main(){
	
	cin >> H>>W;
	while(W!=0){
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				cin >> mp[i][j];
			}
		}
		int c = 0;
		findL();
		while(nx != -1 && ny != -1){
			func(ny,nx);
			c++;
			findL();
		}
		cout << c << endl;
		cin >> W >> H;
	}
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("B", "r", stdin);

typedef pair<int,int> P;

int n, m;
int cnt = 0;
int data[100][100];
int check[100][100];

int mvx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int mvy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void input() {
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++) {
			scanf("%d", &data[i][j]);
		}
	}
	cnt = 0;
	memset(check, 0, sizeof(check));
}

void bfs(int x, int y) {
	queue<P> que;
	que.push(P(x,y));
	check[x][y] = 1;
	while(!que.empty()){
		P cur = que.front(); que.pop();
		for(int i = 0; i < 8; i++) {
			if(cur.first + mvx[i] >= 0 && cur.first + mvx[i] < n && cur.second + mvy[i] >= 0 && cur.second + mvy[i] < m
				&& data[cur.first + mvx[i]][cur.second + mvy[i]] && !check[cur.first+mvx[i]][cur.second+mvy[i]]){
					check[cur.first +mvx[i]][cur.second+mvy[i]] = 1;
					que.push(P(cur.first+mvx[i], cur.second+mvy[i]));
			}
		}
	}
}

void process() {
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(data[i][j] && !check[i][j]) {
				bfs(i,j);
				++cnt;
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	while(true) {
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		input();
		process();
	}
	return 0;
}

C++: #include<cstdio>
#include<utility>
#include<algorithm>
#include<set>
using namespace std;

int w,h;
pair<int,int> g[50][50];
int s[50][51];

void dfs(int x,int y,int xx,int yy){
    g[x][y]=make_pair(xx,yy);
    for(int i=max(0,x-1);i<min(x+2,h);i++){
        for(int j=max(0,y-1);j<min(y+2,w);j++){
            if(s[i][j]&&(g[i][j].first!=xx||g[i][j].second!=yy)){
                dfs(i,j,xx,yy);
            }
        }
    }
}

int main(){
    scanf("%d%d",&w,&h);
    while(w&&h){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&s[i][j]);
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                g[i][j]=make_pair(i,j);
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(s[i][j]==1&&g[i][j].first==i&&g[i][j].second==j){
                    dfs(i,j,i,j);
                }
            }
        }
        set<pair<int,int> > se;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(s[i][j]==1){
                    se.insert(g[i][j]);
                }
            }
        }
        printf("%ld\n",se.size());
        scanf("%d %d",&w,&h);
    }
}

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

int c[51][51];
void dfs(int x,int y){
  int i,j;
  c[x][y]=0;
  for(i=-1;i<=1;++i){
    for(j=-1;j<=1;++j){
      if(c[x+i][y+j]==1){
	dfs(x+i,y+j);
      }
    }
  }
}

int main(){
  int w,h;
  int i,j;

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