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 <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;
 
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
 
const ll MOD=1e9+7;
const ll INF=1e18;
 
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int mp[55][55];
int check[55][55];

int dx2[] = {-1,0,1,1,1,0,-1,-1};
int dy2[] = {1,1,1,0,-1,-1,-1,0};

int main(){

    while(true){
        int w,h;
        int ans = 0;
        cin >> w >> h;
        if(w + h == 0) break;
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                cin >> mp[i][j];
                check[i][j] = 0;
            }
        }
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                if(mp[i][j] == 1 && check[i][j] == 0){
                    ans++;
                    queue<P> que;
                    que.push(P(i,j));
                    while(!que.empty()){
                        P q = que.front(); que.pop();
                        if(check[q.fs][q.sc] != 0) continue;
                        check[q.fs][q.sc] = 1;
                        for(int k = 0;k < 8;k++){
                            int ni = q.fs + dx2[k];
                            int nj = q.sc + dy2[k];
                            if(min(ni,nj) >= 0 && ni < h && nj < w && mp[ni][nj] == 1 && check[ni][nj] == 0){
                                que.push(P(ni,nj));
                            }
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }

}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dh[8] = {0, 1, 1, 1, 0, -1, -1, -1},
    dw[8] = {1, 1, 0, -1, -1, -1, 0, 1};

int main() {
    cout << fixed << setprecision(10);
    
    int w=1, h=1;

    while(w != 0 && h != 0) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        vector<vector<int>> m(h, vector<int>(w));
        rep(i, h) {
            rep(j, w) {
                cin >> m[i][j];
            }
        }
        stack<P> q;
        rep(i, h) {
            rep(j, w) {
                // 到達していない陸じゃなければcontinue
                if (m[i][j] != 1) continue;
                q.emplace(make_pair(i, j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P pp = q.top();
                    q.pop();
                    rep(d, 8) {
                        int nh = pp.first + dh[d], nw = pp.second + dw[d];
                        if (nh >= 0 && nw >= 0 && nh < h && nw < w && m[nh][nw] == 1) {
                            q.emplace(make_pair(nh, nw));
                            m[nh][nw] = 0;
                        }
                    }
                }
                ans += 1;
            }
        }

        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define rep(i,to) for(int i=0;i<to;i++)
using namespace std;

int w,h;
int f[51][51];

int di[] = { 0, 0, 1, 1, 1,-1,-1,-1};
int dj[] = { 1,-1, 0, 1,-1, 0, 1,-1};

void dfs(int i,int j){
	f[i][j] = 0;
	rep(k,8){
		int ni = i + di[k];
		int nj = j + dj[k];
		if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj]==1)
			dfs(ni,nj);
	}
}

int main(void){
	while(cin>>w>>h && (w||h)){
		rep(i,h) rep(j,w) cin>>f[i][j];

		int ans = 0;
		rep(i,h) rep(j,w) if(f[i][j]==1) { dfs(i,j); ans++;}
		cout<<ans<<endl;
	}

	return 0;
}

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

int array[1000][1000];
int X,Y;

void dfs(int y, int x){

  int nx,ny;
  array[y][x]=0;
  for(int dx=-1;dx<=1;dx++)
    for(int dy=-1;dy<=1;dy++){
      nx=x+dx;
      ny=y+dy;
      if(nx<X && ny<Y && nx>=0 && ny>=0 && array[ny][nx]!=0 ) dfs(ny,nx);
    }

}


int main(){

  while(1){
    cin>>X>>Y;
    int cnt=0;
    if(X==0 && Y==0) break;
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	cin>>array[i][j];
    
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	if(array[i][j]==1){ 
	  dfs(i,j);
	  cnt++;
	}
    cout<<cnt<<endl;
  }
  return 0;
}

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

int w, h;
int map[52][52];

int check(int i, int j)
{
	if (map[i][j] == 0)return 0;
	else
	{
		map[i][j] = 0;
		check(i-1, j - 1);
		check(i-1, j);
		check(i-1, j + 1);
		check(i, j - 1);
		check(i, j + 1);
		check(i + 1, j - 1);
		check(i + 1, j);
		check(i + 1, j + 1);
		return 0;
	}
}

int main()
{
	while (true)
	{
		map[52][52] = { 0 };
		int count = 0;
		cin >> w >> h;
		if (w == 0)return 0;

		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				cin >> map[i][j];
			}
		}
		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				if (map[i][j] == 1)
				{
					check(i, j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}