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 <vector>
using namespace std;

vector < vector <int> > V;

int h_max;
int w_max;

void solve( int h, int w )
{
	V[h][w] = 0;
	for ( int i = -1; i < 2; i++ )
	{
		for ( int j = -1; j < 2; j++ )
		{
			if ( i == 0 && j == 0 )
				continue;

			if ( h+i >= 0 && h+i < h_max )
			{
				if ( w+j >= 0 && w+j < w_max )
				{
					if ( V[h+i][w+j] == 1 )
					{
						solve( h+i, w+j );
					}
				}
			}

		}
	}
}

int main ( void )
{
	while (1)
	{
		int w, h;
		cin >> w >> h;
		if ( w == 0 && h == 0 )
			break;

		h_max = h;
		w_max = w;
		V = vector < vector <int> > ( h_max, vector <int>(w_max,0) );

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

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

		cout << kotae << endl;

	}
	return 0;
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<52; ++i) for(int j=0; j<52; ++j) {
			c[i][j] = 0;
			f[i][j] = 1;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 1-c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m = {j, i}, ms;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int map[55][55],w,h;

void d(int y,int x){
  if(y==h||x==w||y==-1||x==-1)return;
  if(map[y][x]==0)return;
  if(map[y][x]==1)map[y][x]=0;
  d(y-1,x-1),d(y+1,x+1),d(y+1,x),d(y,x+1),d(y-1,x),d(y,x-1),d(y-1,x+1),d(y+1,x-1);
}

int main(){
  while(cin>>w>>h,w){
    int ans=0;
    int x,y;
    r(i,h)r(j,w)cin>>map[i][j];
    while(1){
      x=-1;
      r(i,h)r(j,w)if(map[i][j]==1)y=i,x=j;
      if(x==-1)break;
      d(y,x);
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll h,w;
int table[51][51];

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

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

int main(){
    while(cin >> w >> h && w){
        rep(i,h){
            rep(j,w){
                cin >> table[i][j];
            }
        }
        ll ans = 0;
        rep(i,h){
            rep(j,w){
                if(table[i][j]){
                    ans++;
                    solve(i,j);
                }
            }
        }
        cout << ans << endl;
    }
}

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

#define PIAR pair<int, int>
int field[52][52];
int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};

void dfs(int x, int y){
	field[x][y] = 0;
	for(int i = 0; i < 8; i++){
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if(field[nextX][nextY] == 1) dfs(nextX, nextY);
	}
}

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
    	for(int i = 0; i < 52; i++){
    		for(int j = 0; j < 52; j++){
    			field[i][j] = 0;
    		}
    	}

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

        int island = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                	dfs(i, j);
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}