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<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
#define INF 200000000000
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()

bool compare(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;
        
    }else{
        return a.first < b.first;
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0,1,1,-1,-1};
const vector<ll> dy{0,1,0,-1,1,-1,1,-1};


ll cnt=0;
vector<vector<bool>> seen(110,vector<bool>(110));
vector<vector<bool>> seen2(110,vector<bool>(110));
vector<vector<bool>> G;
ll W,H;
void dfs(ll h, ll w){
    seen[h][w] = 1;
    cnt++;
    rep(dir,8){
        ll nh = h + dy[dir];
        ll nw = w + dx[dir];

        if(!In_map(nh,nw,H,W)) continue;
        if(G[nh][nw] == 0) continue;
        if(seen[nh][nw]) continue;
        dfs(nh,nw);
    }



}


int main() {
    while(1){
    seen = seen2;
    G.clear();
    cin>>W>>H;
    if(W==0 && H==0) break;
    ll num=0;
    rep(i,H){
        G.emplace_back();
        rep(j,W){
            bool c;
            cin>>c;
            G[i].pb(c);
        }
    }
    
    ll cnt=0;
    rep(i,H){
        rep(j,W){
            if(G[i][j] == 0) continue;
            if(seen[i][j]) continue;
            dfs(i,j);
            cnt++;
            
        }
    }

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

}

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

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int grid[52][52];
bool visited[52][52];

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

void init(){
    rep(i, 52) rep(j, 52) {grid[i][j] = 0; visited[i][j] = false;}
}

void dfs(int px, int py, int sx, int sy){
    visited[sx][sy] = true;
    rep(i, 8){
        int nx = sx+dx[i];
        int ny = sy+dy[i];
        if(grid[nx][ny] == 1 && !visited[nx][ny]){
            dfs(sx, sy, nx, ny);
        }
    }
}

int main(void){
    while(true){
        int res = 0;
        init();
        int W, H; cin >> W >> H;
        if(W == 0) break;
        rep(i, H){
            rep(j, W){
                cin >> grid[i+1][j+1];
            }
        }
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(grid[i][j] == 1 && !visited[i][j]){
                    dfs(-1, -1, i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

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

int H, W;
void search(int h, int w, vector<vector<int>> &c, vector<vector<bool>> &reached) {
    reached.at(h).at(w) = true;

    for (int direction = 0; direction < 8; ++direction) {
        int nh = h + dx[direction];
        int nw = w + dy[direction];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (c.at(nh).at(nw) == 0) continue;
        if (reached.at(nh).at(nw) == true) continue;
        search(nh, nw, c, reached);
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> W >> H;
        if (H == 0 && W == 0) return 0;
        vector<vector<int>> c(H, vector<int>(W));
        vector<vector<bool>> reached(H, vector<bool>(W));
        rep(i,H) rep(j,W) cin >> c.at(i).at(j);

        rep(i,H) rep(j,W) {
            if (c.at(i).at(j) == 1 && reached.at(i).at(j) == false) {
                ++ans;
                search(i, j, c, reached);
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
using namespace std;

int w, h;
int map[100][100];
int island_number[100][100];

int angle[][2] = {
	{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
	{1, 0}, {1, 1}, {0, 1}, {-1, 1}
};

void dfs(int y, int x)
{
	for (int i = 0; i < 8; i++){
		if (
		y + angle[i][0] >= 0 &&
		y + angle[i][0] < h &&
		x + angle[i][1] >= 0 &&
		x + angle[i][1] < w &&
		island_number[y + angle[i][0]][x + angle[i][1]] == (y + angle[i][0]) * h + x + angle[i][1]
		){
			island_number[y + angle[i][0]][x + angle[i][1]] = island_number[y][x];
			dfs(y + angle[i][0], x + angle[i][1]);
		}
	}
}

int main()
{
	
	while (scanf("%d %d", &w, &h), w + h){
		memset(island_number, 0, sizeof(island_number));
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &map[i][j]);
				if (map[i][j] == 1){
					island_number[i][j] = i * h + j;
				}
				else {
					island_number[i][j] = -1;
				}
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] == i * h + j){
					dfs(i, j);
				}
			}
		}
		
		bool ex[100 * 100] = {false};
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] >= 0){
					ex[island_number[i][j]] = true;
				}
//				printf("%d ", island_number[i][j]);
			}
//			puts("");
		}
		
		int cnt = 0;
		for (int i = 0; i < w * h; i++){
			cnt += ex[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}