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;
const int dx[8]={0,1,0,-1,1,1,-1,-1};
const int dy[8]={1,0,-1,0,1,-1,-1,1};
vector<vector<bool>> seen;
int H,W;
char info[50][50];
void GridDFS(const int y,const int x){
    seen[y][x]=1;
    for(int i=0;i<8;i++){
        int ny=y+dy[i],nx=x+dx[i];
        if(ny<0 or H<=ny or nx<0 or W<=nx)continue;
        if(seen[ny][nx])continue;
        if(info[ny][nx]=='1'){
            GridDFS(ny,nx);
        }
    }
}
void solve(int H,int W){
    seen=vector<vector<bool>>(H,vector<bool>(W));
    for(int i=0;i<H;i++)for(int j=0;j<W;j++)cin>>info[i][j];
    int ans=0;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(seen[i][j])continue;
            if(info[i][j]=='1'){
                ans++;
                GridDFS(i,j);
            }
        }
    }
    printf("%d\n",ans);
}
int main(){
    while(true){
        scanf("%d%d",&W,&H);
        if(W+H==0)break;
        else solve(H,W);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int field[55][55];
    while(true) {
        int H, W;
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        for(int h = 1; h <= H; h++) {
            for(int w = 1; w <= W; w++) {
                cin >> field[h][w];
            }
        }
        UnionFind uni(3001);
        for(int h = 1; h <= H; h++) {
            for(int w = 1; w <= W; w++) {
                if(field[h][w] == 0) continue; 
                for(int dh = -1; dh <= 1; dh++) {
                    for(int dw = -1; dw <= 1; dw++) {
                        int neww = w + dw;
                        int newh = h + dh;
                        if(neww <= 0 || neww > W || newh <- 0 || newh > H) continue;
                        if(field[newh][neww] == 0) continue;
                        uni.merge(h*50+w, newh*50+neww);
                    }
                }
            }
        }
        int ans = 0;
        for(int h = 1; h <= H; h++) {
            for(int w = 1; w <= W; w++) {
                if(field[h][w] == 0) continue;
                if(uni.root(h*50+w) != h*50+w) continue;
                ans++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<random>
#include<map>
#include<queue>
using namespace std;

vector< vector<int> > bfs(int h, int w, int y, int x, vector< vector<int> > t, int g){
  vector< vector<bool> > b(h, vector<bool> (w, false));
  b[y][x] = true;
  queue< pair<int, int> > q;
  q.push( make_pair(x, y) );
  int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
  int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  while(true){
    if( q.empty() ) break;

    pair<int, int> p = q.front();
    q.pop();
    int px = p.first;
    int py = p.second;

    for(int dir=0; dir<8; dir++){
      int tx = px + dx[dir];
      int ty = py + dy[dir];

      if( tx < 0 || tx >= w || ty < 0 || ty >= h ) continue;
      if( b[ty][tx] ) continue;
      if( t[ty][tx] == 0 ) continue;

      b[ty][tx] = true;
      t[ty][tx] = g;
      q.push( make_pair(tx, ty) );
    }
  }

  return t;
}

int main(){

  int w, h;
  while(cin >> w >> h){
    if( w == 0 && h == 0 ) break;
    vector< vector<int> > t(h, vector<int> (w));
    int g = 2;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        cin >> t[i][j];
      }
    }

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if( t[i][j] == 1 ){
          t = bfs(h, w, i, j, t, g);
          g++;
        }
      }
    }
    cout << g-2 << endl;
  }

  return 0;
}