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 <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <array>
#include <map>
#include <set>
using namespace std;

#define IS_POWER_TWO(x) (!(x & (x-1))) 
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

typedef pair<int, int> pii;
const pii Dir_eight[8]{
	{0,-1},{-1,0},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}
};

int main()
{
	int w, h;

	while (scanf("%d %d", &w, &h)) {
		if (w == 0)
			return 0;
		array<array<int, 52>, 52> map = {};
		UnionFind uf(2501);

		for (int i = 0; h > i; i++) {
			for (int j = 0; w > j; j++) {
				int a;
				scanf("%d", &a);

				map[j + 1][i + 1] = a;
				if (!a)uf.unionSet(i*w + j, h*w);
			}
		}

		for (int i = 1; h >= i; i++) {
			for (int j = 1; w >= j; j++) {
				if (map[j][i]) {
					for (int k = 0; 8 > k; k++) {
						if (map[j + Dir_eight[k].first][i + Dir_eight[k].second]) {
							uf.unionSet(j + Dir_eight[k].first + (i + Dir_eight[k].second-1)*w-1, (i-1)*w + j-1);
						}
					}
				}
			}
		}

		set<int> ver;

		for (int i = 0; h*w+1 > i; i++) {
			ver.insert(uf.root(i));
		}
		
		printf("%d\n", ver.size() - 1);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <numeric>
#include <ctime>
#include <bitset>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>

using namespace std;

//define
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX/3)
#define PI (2*acos(0.0))
#define EPS (1e-8)
#define MOD (1000000007LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;

#define vv(t) vector< vector< T > >
#define pb push_back
#define pu push
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define EQ(a,b) (abs((a)-(b))<EPS)

int w, h;
char field[50][50];
typedef pair<int, int> P;

void bfs(int sh, int sw){
  queue<P> que;
  que.push(P(sh, sw));
  while(!que.empty()){
    P p = que.front();
    que.pop();
    int nh = p.first, nw = p.second;
    field[nh][nw] = '0';
    for(int i = -1; i <= 1; i++){
      for(int j = -1; j <= 1; j++){
        if(i == 0 && j == 0) continue;
        int th = nh + i, tw = nw + j;
        if(th < 0 || th >= h || tw < 0 || tw >= w || field[th][tw] != '1') continue;
        que.push(P(th, tw));
      }
    }
  }
}

int main(void){
  ios_base::sync_with_stdio(0);
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++) cin >> field[i][j];
    }
    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(field[i][j] == '0') continue;
        bfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0; 
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[50][50], f[50][50];
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<h; ++i) for(int j=0; j<w; ++j) {
			cin >> c[i][j];
			f[i][j] = 0;
		}
		for(int i=0; i<h; ++i) for(int j=0; j<w; ++j) {
			mapst m = {j, i}, ms;
			if(func(j,i)) {
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					
					f[m.y][m.x] = 1;
					if(m.x>0&&m.y>0) if(func(m.x-1,m.y-1)) {
						ms.x = m.x-1; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.y>0) if(func(m.x,m.y-1)) {
						ms.x = m.x; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.x<w-1&&m.y>0) if(func(m.x+1,m.y-1)) {
						ms.x = m.x+1; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.x<w-1) if(func(m.x+1,m.y)) {
						ms.x = m.x+1; ms.y = m.y;
						q.push(ms);
					}
					if(m.x<w-1&&m.y<h-1) if(func(m.x+1,m.y+1)) {
						ms.x = m.x+1; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.y<h-1) if(func(m.x,m.y+1)) {
						ms.x = m.x; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.x>0&&m.y<h-1) if(func(m.x-1,m.y+1)) {
						ms.x = m.x-1; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.x>0) if(func(m.x-1,m.y)) {
						ms.x = m.x-1; ms.y = m.y;
						q.push(ms);
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stacK>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}