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()
}

C++: #include <iostream>

using namespace std;

const int MAX_SIZE = 51;

int field[MAX_SIZE][MAX_SIZE];
int w, h;

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

bool ok(int y, int x) {
	return x >= 0 && x < w && y >= 0 && y < h;
}

void consume(int y, int x) {
	bool flag = false;
	for (int i = 0; i < 8; ++i) {
		int curx = x + dx[i],
			cury = y + dy[i];
		if (ok(cury, curx) && field[cury][curx] == 1) {
			field[cury][curx] = 0;
			consume(cury, curx);
		}
	}
}

int main() {
	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 >> field[i][j];

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

C++: #include<iostream>
using namespace std;
#define MAX (256)

int field[MAX][MAX];
int ans = 0;

void dfs(int i, int j){
	for(int k = -1; k <= 1; k++){
		for(int l = -1; l <= 1; l++){
			if(field[i+k][j+l] == 1){
				field[i+k][j+l] = 0;
				dfs(i+k, j+l);
			}
		}
	}
}

int main(){
	int k, l;

	while(1){
		ans = 0;
		cin >> l >> k;

		if((k|l) == 0) break;

		for(int i=0; i < MAX; i++){
			for(int j=0; j < MAX; j++) field[i][j]= 0;
		}

		for(int i=0; i < k; i++){
				for(int j=0; j < l; j++){
					cin >> field[i][j];
			}
		}

		for(int i=0; i < k; i++){
			for(int j=0; j < l; j++){
				if(field[i][j] == 1){
					field[i][j] = 0;
					dfs(i, j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
#define REP(i, n, N) for(ll i=(n); i < N; i++)
#define CK(N, A, B) (A <= N && N < B)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << ' ' << (b) << endl
using namespace std;
typedef long long ll;

const int NODE_SIZE=10101;
int H;
int W;
int start_y, start_x;
int island_num=2;

int field[NODE_SIZE][NODE_SIZE];

int d[NODE_SIZE][NODE_SIZE];

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

void bfs(){
    queue<pair<int,int>> q;
    bool visited[H][W];
    REP(i,0,H) REP(j,0,W) visited[i][j]=false;
    REP(i,0,H) REP(j,0,W) d[i][j]=-1;
    q.push({start_y, start_x});
    d[start_y][start_x]=0;
    while(!q.empty()){
        int cur_y = q.front().first;
        int cur_x = q.front().second;
        int curStep = d[cur_y][cur_x];
        q.pop();
        if(visited[cur_y][cur_x]) continue;
        visited[cur_y][cur_x] = true;
        field[start_y][start_x]=island_num;

        REP(k,0,8){
            int next_y = cur_y + dy[k];
            int next_x = cur_x + dx[k];

            if(!CK(next_y,0,H) || !CK(next_x,0,W))continue;
            if(field[next_y][next_x]==1){
                field[next_y][next_x]=island_num;
            }else{
                continue;
            }
            if(!visited[next_y][next_x]){
                q.push({next_y, next_x});
                d[next_y][next_x] = d[cur_y][cur_x] + 1;
            }
        }
    }
}

int main() {
    while (cin >> W >> H && W){
        island_num = 2;
        REP(i,0,H){
            REP(j,0,W){
                cin >> field[i][j];
            }
        }
        REP(i,0,H){
            REP(j,0,W){
                if(field[i][j]==1){
                    start_y=i,start_x=j;
                    bfs();
                    island_num++;
                }
            }
        }
        p(island_num-2);
    }

    return 0;
}

C++: #include <iostream>
using namespace	std;
int h,w,c[50][50];
void dfs(int y,int x){
  c[y][x]=0;
  for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++)
      if(0<=y+i&&y+i<h&&0<=x+j&&x+j<w)
        if(c[y+i][x+j]) dfs(y+i,x+j);
}

int main(){

  for(;cin>>w>>h,w;){
    int r=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++){
        if(c[i][j]){
          r++;
          dfs(i,j);
        }
      }
    cout<<r<<endl;
  }
  return 0;

}

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

void search(int y, int x, int h, int w, int** field) {
    field[y][x] = 0;
    for (int ny = y - 1; ny <= y + 1; ny++) {
        for (int nx = x - 1; nx <= x + 1; nx++) {
            if (0 <= ny && ny < h && 0 <= nx && nx < w && field[ny][nx] == 1) {
                search(ny, nx, h, w, field);
            }
        }
    }
}

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if ((w | h) == 0) {
            break;
        }
        int** field = new int* [h];
        for (int i = 0; i < h; i++) {
            field[i] = new int [w];
        }
        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) {
                    search(y, x, h, w, field);
                    count++;
                }
            }
        }
        cout << count << endl;
        for (int i = 0; i < h; i++) {
            delete[] field[i];
        }
        delete[] field;
    }
}