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>
#include <queue>
using namespace std;
const int dx[]={1,0,-1,0,1,-1,1,-1}, dy[]={0,-1,0,1,1,-1,-1,1};

bool valid(int x,int y,int W,int H)
{
    return (x>=0&&x<W)&&(y>=0&&y<H);
}
int main()
{
    int W,H,i,j,k,x,y;
    queue<int> Q;
    cin >> H >> W;
    while(W && H)
    {
        int num=0;
        int A[W][H];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
                cin >> A[i][j];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
            {
                if(A[i][j]==1)
                {
                    Q.push(i);
                    Q.push(j);
                    A[i][j]=0;
                    num++;
                    while(!Q.empty())
                    {
                        x=Q.front();
                        Q.pop();
                        y=Q.front();
                        Q.pop();
                        for(k=0;k<8;k++)
                        {
                            if(valid(x+dx[k],y+dy[k],W,H))
                            {
                                if(A[x+dx[k]][y+dy[k]]==1)
                                {
                                    Q.push(x+dx[k]);
                                    Q.push(y+dy[k]);
                                    A[x+dx[k]][y+dy[k]]=0;
                                }
                            }
                        }
                    }
                }
            }
        cout << num << endl;
        cin >> H >> W;
    }
    return 0;
}

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

void	dfs(int w, int h, int grid[50][50], bool seen[50][50],
			int dx[], int dy[], int x, int y)
{
	seen[y][x] = true;
	int	nx, ny;
	for (int i = 0; i < 8; ++i) {
		nx = x + dx[i];
		ny = y + dy[i];
		if (nx < 0 || w <= nx || ny < 0 || h <= ny)
			continue;
		if (grid[ny][nx] == 0)
			continue;
		if (seen[ny][nx])
			continue;
		dfs(w, h, grid, seen, dx, dy, nx, ny);
	}
}

int	main()
{
	int		w, h;
	int		grid[50][50];
	bool	seen[50][50];
	int		dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
	int		dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
	while (cin >> w >> h) {
		if (w == 0 && h == 0)
			return 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> grid[i][j];
				seen[i][j] = false;
			}
		}
		int	num_island = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (grid[i][j] == 0)
					continue;
				if (seen[i][j])
					continue;
				++num_island;
				dfs(w, h, grid, seen, dx, dy, j, i);
			}
		}
		cout << num_island << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <stack>
using namespace std;
int grid[50][50];
int w, h, ans;
void dfs(int i, int j){ //iが縦jが横
    if(i<0||j<0||grid[i][j]==0) return ;
    grid[i][j]=0;
    
    for(int n=-1; n<=1; ++n){
        for(int m=-1; m<=1; ++m){
            dfs(i+n, j+m);
        }
    }
    return ;
}
int main() {
    while(cin >> w >> h, w||h){
        
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> grid[i][j];
            }
        }
        ans = 0;
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(grid[i][j]==1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
        
    return 0;
}

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

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

int W, H, maze[64][64];
bool visited[64][64];

bool check(int x, int y){
	return x >= 0 && x < W && y >= 0 && y < H && maze[y][x] == 1 && !visited[y][x];
}

void dfs(int x, int y){
	visited[y][x] = true;
	
	for(int i = 0; i < 8; ++i){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(check(nx, ny)){
			dfs(nx, ny);
		}
	}
	return;
}

int main() {
	while(cin >> W >> H && (W || H)){
		memset(visited, 0, sizeof(visited));
		
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				scanf("%d", &maze[i][j]);
			}
		}
		int ans = 0;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				if(maze[i][j] == 1 && !visited[i][j]){
					ans++;
					dfs(j, i);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int w, h;
int graph[50][50];
int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

void solve(int y, int x) {
    graph[y][x] = 0;
    for(int i = 0; i < 8; ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny >= 0 && ny < h && nx >= 0 && nx < w && graph[ny][nx]) {
            solve(ny, nx);
        }
    }
}

int main() {

    while(cin >> w >> h, w | h) {

        int cnt = 0;

        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                cin >> graph[i][j];
            }
        }

        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                if(graph[i][j]) {
                    solve(i, j);
                    ++cnt;
                }
            }
        }

        cout << cnt << endl;

    }

}