Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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