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;
using ll=long long;
typedef pair<int,int> P;
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; }
int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    queue<P> Q;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0,x,y,nx,ny;
    P p;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
            Q.push(P(i,j));
            while(!Q.empty()){
        p=Q.front();
        Q.pop();
        x=p.first;
        y=p.second;
        G.at(x).at(y)=0;
            for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            nx=x+i;
            ny=y+j;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) Q.push(P(nx,ny));
        }
    }
    }
    count++;
    }
    }
    }
cout<<count<<endl;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

//関数プロトタイプ宣言
bool input();
void bfs(int H, int W);

//変数宣言
int w, h;
bool chart[50][50];		//trueは陸、falseは海
bool check[50][50];		//探索を終えた場所はtrue
typedef pair<int, int> P;	//P(h, w)に統一する
int dw[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dh[8] = {-1, 0, 1, -1, 1, -1, 0, 1};	
//順に　左上　←　左下　↑　↓　右上　→　右下

int main()
{
	while (input())
	{
		int output = 0;
		//check配列の初期化
		for (int i=0; i<h; i++)
			for (int i2=0; i2<w; i2++)
				check[i][i2] = false;
	
		//陸の探索
		for (int i=0; i<h; i++)
		{
			for (int i2=0; i2<w; i2++)
			{
				//次のbfsで１つの島の全ての陸のcheckをtrueにする
				if (chart[i][i2]==true)
				{
					bfs(i, i2);		//引数は(h, w)で渡している
					output++;
				}
			}
		}
		printf("%d\n", output);
	}
	return 0;
}


//こいつにある陸の座標を渡すと、陸続きの全ての陸のcheck値がtrueになる
void bfs(int H, int W)
{
	queue<P> que;
	//スタート地点の陸の座標をキューに入れる
	que.push(P(H, W));
	check[H][W] = true;
	chart[H][W] = false;
	
	//キューが空になるまでループ
	while (que.size())
	{
		// printf("in while\n");
		
		//キューの先頭を取りだす
		P p = que.front();
		que.pop();
		
		//移動８方向をループ
		for (int i=0; i<8; i++)
		{
			//移動後の座標をnw, nhとする
			int nw = p.second + dw[i];
			int nh = p.first + dh[i];
			
			if (0<=nw && nw<w && 0<=nh && nh<h && chart[nh][nw]==true && check[nh][nw]==false)
			{
				que.push(P(nh, nw));
				check[nh][nw] = true;
				chart[nh][nw] = false;
			}
		}
	}
}

bool input()
{
	scanf("%d %d", &w, &h);
	if (w==0) return false;

	for (int i=0; i<h; i++)
	{
		for (int i2=0; i2<w; i2++)
		{
			//陸はtrue、海はfalseで入る
			cin >> chart[i][i2];
			// printf("%d ", chart[i][i2]);
		}
		// printf("\n");
	}
	return true;
}

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

// 入力
int H, W;
vector<vector<int>> field(H, vector<int>(W, 0));

// 探索
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, nw = w + dw;

            // 場外アウトしたり、0 だったりはスルー
            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 count = 0;
        for (int h = 0; h < H; ++h)
        {
            for (int w = 0; w < W; ++w)
            {
                if (field[h][w] == 0)
                    continue; // 残島じゃなかったらスルー
                dfs(h, w);
                ++count;
            }
        }
        cout << count << endl;
    }
}

C++: #include <iostream>
#include <vector>

int flag[52][52];
int c[52][52];
std::vector<std::vector<int> > land;
std::vector<int> v(2);
int island;
int w, h;

bool search(int x, int y){
  flag[x][y] = 1;
  for(int dx = -1; dx < 2; dx++){
    for(int dy = -1; dy < 2; dy++){
      if(c[x + dx][y + dy] == 1 && flag[x + dx][y + dy] == 0){
        v[0] = x + dx;
        v[1] = y + dy;
        land.push_back(v);
        flag[x + dx][y + dy] = 1;
      }
    }
  }
  land.erase(land.begin());
  return land.empty();
}

int main(){
  while(true){
    std::cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }
    else{
      island = 0;
      for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
          std::cin >> c[j][i];
          flag[j][i] = 0;
        }
      }

      for(int i = 1; i <= w; i++){
        for(int j = 1; j <= h; j++){
          if(c[i][j] == 1 && flag[i][j] == 0){
            v[0] = i;
            v[1] = j;
            land.push_back(v);
            while(true){
              if(search(land[0][0], land[0][1])) break;
            }
            island++;
          }
        }
      }
      std::cout << island << std::endl;
    }
  }
  return 0;
}