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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e7+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
int c[100][100];
int ans;
void dfs(int y, int x){
	c[y][x] = 0;
	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int ny = y + i, nx = x + j;//??????8???????????§?¨?
			if (ny >= 0 && ny < h&&nx >= 0 && nx < w){//?????????????????????
				//???????????£???????????¶???????????????????????°
				if (c[ny][nx] == 1)dfs(ny, nx);
			}
		}
	}
}

int main(){
	while (cin >> w >> h&&w + h){
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) cin >> c[i][j];
		ans = 0;
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (c[i][j] == 1){//??¶??????????????????
					solve(i, j);//???????????£???????????¶????????¶???
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define Graph vector<vector<int>>
using namespace std;

void DFS(const Graph &G,int u,int v,vector<vector<bool>> &seen,vector<int> y, vector<int> x,int h,int w){//深さ優先探索。
//引数は順にグラフ、始点(注目点)、探索済みかどうか、y変位、x変位、y方向のグリッドサイズ、x方向のグリッドサイズ
  
  seen[u][v]=true;

  rep(k,8){
    int yd=u+y[k],xd=v+x[k];
    if(yd<0||h<=yd||xd<0||w<=xd) continue;//グラフの外側はスルー
    if(seen[yd][xd]) continue;//もう探索した場所はスルー
    if(G[yd][xd]==0) continue;//陸地でないならスルー
    DFS(G,yd,xd,seen,y,x,h,w);
  }
  return ;
}
 
int main() {

  while(1){

  int w,h;  cin >> w >> h ;

  if(w==0&&h==0) break;

  Graph G(h,vector<int> (w));//Grid

  rep(i,h){
    rep(j,w){
    cin >> G[i][j];
    }
  }//入力終了

  vector<vector<bool>> seen(h,vector<bool>(w,false));//探索済み
  int ans=0;//島の数

  vector<int> x={1,-1,0,0,1,1,-1,-1};
  vector<int> y={0,0,1,-1,-1,1,1,-1};

  rep(i,h){
    rep(j,w){
      if(seen[i][j]) continue;
      if(G[i][j]==0) continue;
      DFS(G,i,j,seen,y,x,h,w);

      ans++;//DFS一つ終わるたびに島+1
    }
  }
    cout << ans << endl;
  
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
???
using ll=long long;
#define int ll
???
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
???
int read(){
	int i;
	scanf("%lld",&i);
	return i;
}
???
int land[100][100]={0};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;
???
void isLand(int i,int j){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newx=j+dx[k];
			int newy=i+dy[k];
			isLand(newy,newx);
		}
    }
}
???
signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;
vector<vector<int> > field;

// 深さ優先探索
bool seen[60][60];
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[9] = {0, 1, 0, -1, 1, -1, 1, -1};
int w,h;

void dfs(int current_h, int current_w){
    seen[current_h][current_w] = true;

    for(int dir=0; dir < 8; dir++){
        int next_w = current_w + dx[dir];
        int next_h = current_h + dy[dir];

        if(next_w < 0) continue;
        if(next_h < 0) continue;
        if(next_w >= w) continue;
        if(next_h >= h) continue;
        
        if(field[next_h][next_w] == 1 && !seen[next_h][next_w]){
            dfs(next_h, next_w);
        }
    }
}

int main(){
    while(true) {
        cin >> w >> h;
        memset(seen, 0, sizeof(seen));
        if( w==0 && h==0 ){
            break;
        }

        field.assign(h, vector<int>(w, 0));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> field[i][j];
            }
        }

        int count = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(field[i][j] ==1 && !seen[i][j] ){
                    dfs(i, j);
                    count ++;
                }
            }
        }

        cout << count << endl;
    }
}

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

const int X[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int Y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int W, H;
vector<vector<int>> Field;

void dfs(int y, int x){
    Field[y][x] = 0;
    for(int i = 0; i < 8; i++){
        int NeX = x + X[i];
        int NeY = y + Y[i];
        if(NeX < 0 || W <= NeX || NeY < 0 || H <= NeY)continue;
        if(Field[NeY][NeX] == 0)continue;
        dfs(NeY, NeX);
    }
}

int main(){
    while(cin >> W >> H){
        if(W == 0 && H == 0)break;
        Field.assign(H, vector<int>(W, 0));
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> Field[i][j];
            }
        }
        
        int ans = 0;
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(Field[i][j] == 0)continue;
                dfs(i, j);
                ans++;
            }
        }
        
        cout << ans << endl;
    }
    return 0;
}