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