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;

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

int map[51][51];
int w, h;

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	if (x < 0 || w <= x) return;
	if (y < 0 || w <= y) return;
	if (map[y][x] == 0) return;
	map[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int W, H;
vector<vector<int>> field;

void dfs(int h, int w) {
    field[h][w] = 0;
    
    for (int dh = -1; dh <= 1; dh++) {
        for (int dw = -1; dw <= 1; dw++) {
            int nh = h + dh;
            int nw = w + dw;
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == 0) continue;

            dfs(nh, nw);
        }
    }
}

int main() {
    while(cin >> W >> H) {
        if (H == 0 && W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                cin >> field[h][w];
            }
        }

        int cnt = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (field[h][w] == 0) continue;
                dfs(h, w);
                cnt++;
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

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

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

int w, h;

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

int main() {
	while (cin >> w >> h, w | h) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> field[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (field[i][j] == 1) {
					crawler(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int x, int y){
	field[y][x] = 3;

	for(int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 and nx <= w-1 and ny >= 0 and ny <= h and field[ny][nx]==1){
			dfs(nx, ny);
		}
	}
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

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

C++: #include <stdio.h>
#include <string.h>
#define rep(i, n) for (int i =  0; i < (int)(n); i++)

int n, m, f[64][64];

void rec(int x, int y) {
    if (f[x][y] == 0) return ;
    f[x][y] = 0;
    rep (i, 3) rep (j, 3) rec(x+i-1, y+j-1);
}

int main() {
    for (;;) {
        scanf("%d%d", &m, &n);
        if (n == 0) return 0;
        memset(f, 0, sizeof(f));
        rep (i, n) rep (j, m) scanf("%d", f[i+1]+j+1);
        int c = 0;
        rep (i, n) rep (j, m) if (f[i+1][j+1]) c++, rec(i+1, j+1);
        printf("%d\n", c);
    }
}

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

int H,W;

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

bool seen[51][51];
int board[51][51];

  void dfs(int h, int w) {
  seen[h][w] = true; 
  
  for (int dir = 0; dir < 8; ++dir) {
    int nw = w + dx[dir];
    int nh = h + dy[dir];  
    if (nw < 0 || nw >= W || nh < 0 || nh >= H) continue;
    if (seen[nh][nw]) continue; 
    if (board[nh][nw] == 1) dfs(nh,nw);
  }
} 

int main() {  
  cin >> W >> H;
  while (W >= 1 || H >= 1) {
    int ans =0;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        int tmp; cin >> tmp;
        board[i][j] = tmp;
      }
    }
    
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        seen[i][j] = false;
      }
    }
  
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        if (seen[i][j] == true || board[i][j] == 0) continue;
        dfs(i,j);
        ans++;
      }
    } 
    cout << ans << endl; 
    cin >> W >> H;
    for (int i =0; i<H; i++) {
      for (int j =0; j<W; j++) {
        board[i][j] = 0;
      }
    }
  }
}