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;

template <class T>
void readVector(int n, vector<T> &v){
	for(int i=0; i<n; ++i){
		T t;
		cin >> t;
		v.push_back(t);
	}
}
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
typedef pair<long long, long long> P;

void solve(int w, int h){
	vector<int> v[h];
	for(int i=0; i<h; ++i)
		readVector(w, v[i]);
	int ans = 0;
	for(int i=0; i<h; ++i)
		for(int j=0; j<w; ++j){
			if(v[i][j] == 1){
				++ans;
				v[i][j] = 0;
				queue<P> q;
				q.push(P(i, j));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					int nx = p.first;
					int ny = p.second;
					for(int k=0; k<8; ++k)
						if(0<=nx+dx[k] && nx+dx[k]<h && 0<=ny+dy[k] && ny+dy[k]<w && v[nx+dx[k]][ny+dy[k]] == 1){
							v[nx+dx[k]][ny+dy[k]] = 0;
							q.push(P(nx+dx[k], ny+dy[k]));
						}
				}
			}
		}
	cout << ans << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		solve(w, h);
	}
}

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

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; 

void bfs(int sx, int sy)
{
  queue<pxy> q;
  q.push(pxy(sx, sy)); 
  while(!q.empty()){
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if(map[x][y]){
      map[x][y] = 0;
      for(int i = 0; i < 8; i++){
	int next_x = x + dx[i], next_y = y + dy[i];
	if(map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h && next_y >= 0){
	  q.push(pxy(next_x, next_y));
	}
      }
    }
  }
}

int main()
{
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }else{
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  cin >> map[j][i];
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  if(map[j][i]){
	    ans++;
	    bfs(j, i);
	  }
      cout << ans << endl;
      ans = 0;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};
bool seen[50][50];
void dfs(vector<vector<int>> C, int h, int w, int a, int b){
    seen[a][b]=true;
    for (int i=0; i<8; i++) {
        int aa=a+dx[i];
        int bb=b+dy[i];
        if (aa<0 || bb<0 || aa>=h || bb>=w) continue;
        if (C[aa][bb]==0) {
            continue;
        }
        if (seen[aa][bb]==true) continue;
        dfs(C,h,w,aa,bb);
    }
}

int main() {
    int w,h;
   cin >> w >> h;
   int total=0;
   vector<int> x;
   while(w!=0 || h!=0) {
       total++;
   vector<vector<int>> C(h,vector<int>(w));
   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           int p;
           cin >> p;
           C[i][j]=p;
       }
   }
   int count=0;

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           seen[i][j]=false;
       }
   }

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           if (seen[i][j]==true) continue;
           if (C[i][j]==0) continue;
           count++;
           dfs(C,h,w,i,j);
           
       }
   }


   x.push_back(count);
   int a,b;
   cin >> a >> b;
   w=a,h=b;
   }
   for (int i=0; i<total; i++) {
       cout << x[i] << endl;
   }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<queue>
#include<sstream>
#include<iomanip>

#define reps(i,j,k) for(int i=(j);i<(k);i++)
#define rep(i,j) reps(i,0,j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class S,class T>
ostream &operator<<(ostream &out, const pair<S,T> p){
	return out << "(" << p.fs << ", " << p.sc << ")";
}

template<class T>
ostream &operator<<(ostream &out, const vector<T> &v){
	out << "{";
	rep(i,v.size()) out << v[i] << ", ";
	return out << "}" << endl;
}
int c[50][50];
int main(){
	int w,h;
	int dx[] = {-1,  0,  1, -1, 1, -1, 0, 1};
	int dy[] = {-1, -1, -1,  0, 0,  1, 1, 1};
	while(cin >> w >> h, w){
		rep(i,h) rep(j,w) {
			cin >> c[i][j];
		}
		int cnt = 2;
		rep(i,h) rep(j,w){
			if(c[i][j] != 1) continue;
			c[i][j] = cnt;
			queue<pii> q;
			q.push(pii(i, j));
			while(!q.empty()){
				int x = q.front().sc, y = q.front().fs;
				q.pop();
				rep(k,8){
					int cx = x + dx[k], cy = y+dy[k];
					if(cx >= 0 && cy >= 0 && cy < h && cx < w && c[cy][cx] == 1){
						q.push(pii(cy, cx));
						c[cy][cx] = cnt;
					}
				}
			}
			cnt ++;
		}
		cout << cnt - 2 << endl;
	}
	return 0;
}