Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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;
    }
}