Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;

    }

}