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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

int w,h;

int main()
{
    while(cin >> w >> h,w)
    {
        vector<vector<int>> fie(h,vector<int>(w,0));
        rep(i,0,h - 1)rep(j,0,w - 1) cin >> fie[i][j];
        int res = 0;
        rep(i,0,h - 1)rep(j,0,w - 1){
            if(fie[i][j])
            {
                queue<P> que;
                res++;
                que.push({i,j});
                fie[i][j] = 0;
                while(!que.empty())
                {
                    auto p = que.front();
                    que.pop();

                    for(int i = p.first - 1;i <= p.first + 1;i++)
                    {
                        for(int j = p.second - 1;j <= p.second + 1;j++)
                        {
                            if(0 <= i && i < h && 0 <= j && j < w && fie[i][j])
                            {
                                que.push({i,j});
                                fie[i][j] = 0;
                            }
                        }
                    }
                }
            }
        }
        cout << res << endl;
    }
}

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

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

void dfs(int y, int x, vector< vector<int> > &v){
    v[y][x] = 0;

    for(int i = 0 ; i < 8 ; i++){
        int ny = y + dy[i], nx = x + dx[i];
        if(v[ny][nx] == 1){
            dfs(ny, nx, v);
        }
    }

    return;
}

int main(){
    int w, h;
    while(cin >> w >> h, w){
        vector< vector<int> > c(h + 2, vector<int>(w + 2, 0));
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                cin >> c[i][j];
            }
        }

        int ans = 0;
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                if(c[i][j] == 1){
                    ans++;
                    dfs(i, j, c);
                }
            }
        }

        cout << ans << endl;
    }
}

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

int main(){
  int w,h;
  int dx[8] = {1,1,0,-1,-1,-1,0,1};
  int dy[8] = {0,1,1,1,0,-1,-1,-1};
  while(true){
  cin>>w>>h;
  if(!w&&!h)break;
  w+=2,h+=2;
  int island[w][h];
  for(int x=0;x<w;x++){
    island[x][0] = 0;
    island[x][h-1] = 0;
  }
  for(int y=1;y<h-1;y++){
    island[0][y] = 0;
    island[w-1][y] = 0;
    for(int x=1;x<w-1;x++){
      cin>>island[x][y];
    }
  }
  int count = 0;
  queue<int> qu;
  for(int y=1;y<h-1;y++){
    for(int x=1;x<w-1;x++){
      if(island[x][y]){
	qu.push(y*w+x);
	while(!qu.empty()){
	  if(island[qu.front()%w][qu.front()/w]){
	    island[qu.front()%w][qu.front()/w] = 0;
	    for(int i=0;i<8;i++)
	      qu.push(qu.front()+dy[i]*w+dx[i]);
	  }
	  qu.pop();
	}
	count++;
      }
    }
  }
  cout<<count<<endl;
  }
  return 0;
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+1, y);
    dfs(x+1, y+2);
    dfs(x+1, y-2);
    dfs(x-1, y);
    dfs(x-1, y+2);
    dfs(x-1, y-2);
    dfs(x, y+2);
    dfs(x, y-2);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            cin.getline(c[k], sizeof(c[k]));
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define X first
#define Y second

using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int cnt, w, h;
int m[100][100];
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void dfs(pii);

int main(void){
    for(;cin >> w >> h, w; cout << cnt << endl){
        cnt = 0;
        rep(y, h) rep(x, w) cin >> m[y][x];
        rep(y, h) rep(x, w) if(m[y][x] == 1) cnt ++, dfs(mp(x, y));
    }
 
    return 0;
}

void dfs(pii p){
    if(p.X < 0 || w <= p.X ||
       p.Y < 0 || h <= p.Y) return;
    
    if(m[p.Y][p.X] == 1) m[p.Y][p.X] = 0;
    else return;

    rep(i, 8) dfs(mp(p.X + dx[i], p.Y + dy[i]));
}