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

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

// 探索
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;
            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>
using namespace std;
 
int main(void){
    while (1) {
        int Y, X;
        int c[55][55];
        cin >> X >> Y;
         
        //終了
        if (Y == 0 && X == 0) break;
         
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) cin >> c[i][j];
        }
         
        //深さ別にリストを作成
        int vy[2555][155], vx[2555][155], pl[2555];
        for (int i = 0; i < 2000; i++) {
            pl[i] = 0;
            vy[i][0] = vx[i][0] = -1;
            for (int j = 1; j <= 553; j++) {
                vy[i][j] = vx[i][j] = 0;
            }
        }  
 
        //移動方向の指定
        int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
        int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
     
        //通過済みかどうか
        bool psd[55][55];
        
        for (int i = 0; i <= 53; i++) {
            for (int j = 0; j <= 53; j++) psd[i][j] = false;
        }
        
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) {
                psd[i][j] = true;
                if (c[i][j] == 0) psd[i][j] = false;
            }
        }
         
        for (int i = 0; i <= Y + 1; i++) psd[i][0] = psd[i][X + 1] = false;
        for (int i = 0; i <= X + 1; i++) psd[0][i] = psd[Y + 1][i] = false;
         
        bool fns = false;
        int isl = 0;
        while (1) {
            for (int inf = 0; true; inf++) {
                bool spot = false;
                if (inf == 0) {
                    vy[0][0] = -1;
                    for (int i = 1; i <= Y; i++) {
                        for (int j = 1; j <= X; j++) {
                            if (psd[i][j] == true) {
                                vy[0][0] = i;
                                vx[0][0] = j;
                                isl++;
                                pl[0] = 1;
                                psd[i][j] = false;
                                spot = true;
                                break;
                            }
                        }
                        if (spot) break;
                    }
                    if (vy[0][0] == -1) {
                        fns = true;
                        break; //4, 全部の島の確認終了
                    }
                }
                else {
                    int pt = 0;
                    for (int i = 0; i < pl[inf - 1]; i++) {
                        for (int dir = 0; dir < 8; dir++) {
                            
                            if (vy[inf - 1][i] + dy[dir] != -1 && vy[inf - 1][i] + dy[dir] != Y + 2 && vx[inf - 1][i] + dx[dir] != -1 && vx[inf - 1][i] + dx[dir] != X + 2) {
                                if (psd[vy[inf - 1][i] + dy[dir]][vx[inf - 1][i] + dx[dir]] == true) {
                                    vy[inf][pt] = vy[inf - 1][i] + dy[dir];
                                    vx[inf][pt] = vx[inf - 1][i] + dx[dir];
                                    pl[inf]++;
                                    psd[vy[inf][pt]][vx[inf][pt]] = false;
                                    pt++;
                                }
                            }
                        }
                    }
                    if (pt == 0) break; //4, 1つの島の確認終了
                }
//                cout << "*" << inf << endl;
//                for (int i = 0; i < pl[inf]; i++) cout << vy[inf][i] << ", " << vx[inf][i] << endl;
            }
            if (fns) {
                cout << isl << endl;
                break; // 2, query終了
            }
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    int a[h][w];
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
      }
    }
    int ans=0;
    queue<pair<int,int>> q;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(a[i][j]&&!used[i][j]){
          //cout<<i<<" "<<j<<endl;
          ans++;
          q.push(make_pair(i,j));
          while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int x=p.first, y=p.second;
            if(used[x][y]) continue;
            used[x][y]=1;
            for(int i=-1;i<2;i++){
              for(int j=-1;j<2;j++){
                if(x+i==-1||x+i==h) continue;
                if(y+j==-1||y+j==w) continue;
                if(a[x+i][y+j]&&!used[x+i][y+j]){
                  q.push(make_pair(x+i,y+j));
                }
              }
            }
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}