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 <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef vector<vector<int> > VII;
typedef pair<int, int> PII;

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

void bfs (int i, int j, VII &table) {
    queue<PII> que;
    que.push(make_pair(i, j));
    table[i][j] = 0;
    while (1) {
        if (que.empty() == 1) {
            break; 
        }
        int now_y = que.front().first;
        int now_x = que.front().second;
        que.pop();

        for (int k = 0; k < 8; k++) {
            if (table[now_y + dy[k]][now_x + dx[k]] == 1) {
                que.push(make_pair(now_y + dy[k], now_x + dx[k]));
                    table[now_y + dy[k]][now_x + dx[k]] = 0;
            }
        }
    }
    return;
}

int main(void) {
    int w;
    int h;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) {
            break;
        }
        VII table(h + 2, vector<int>(w + 2, 0));
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                if (table[i][j] == 1) {
                    bfs(i, j, table);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}

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

using ll = long long;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vb = vector<bool>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
#define rep(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define vrep(i, vec) for(auto& i : vec)
#define vin(vec) for(auto& i : vec) cin >> i
#define all(v) v.begin(), v.end()
ll mod = 1000000007;

ll w, h;
vvll graph;

void dfs(vvll &graph, ll x, ll y){
  graph[y][x] = 2;
  rep(dx, -1, 2) rep(dy, -1, 2)if(x + dx >= 0 && x + dx < w && y + dy >= 0 && y + dy < h)if(graph[y + dy][x + dx] == 1) dfs(graph, x + dx, y + dy);
}

int main(){
  while(true){
  cin >> w >> h;
  if(w == 0 && h == 0) break;
  graph.assign(h, vll(w));
  rep(y, 0, h)rep(x, 0, w) cin >> graph[y][x];
  ll ans = 0;
  rep(y, 0, h)rep(x, 0, w){
    if(graph[y][x] == 1){
      dfs(graph, x, y);
      ans++;
    }
  }
  cout << ans << endl;}
}

C++: #include <stdio.h>

int W,H,ans;
int map[100][100];

void search_island(int x, int y){
	map[x][y] = 0;
	if(x > 0){
		if(map[x - 1][y]){
			search_island(x - 1, y);
		}
	}
	if(x < H - 1){
		if(map[x + 1][y]){
			search_island(x + 1, y);
		}
	}
	if(y > 0){
		if(map[x][y - 1]){
			search_island(x, y - 1);
		}
	}
	if(y < W - 1){
		if(map[x][y + 1]){
			search_island(x, y + 1);
		}
	}
}


int main()
{
	scanf("%d %d",&W,&H);
	while(W != 0 && H != 0){
		for(int i = 0; i < H; i++){
			for(int k = 0; k < W; k++){
				scanf("%d",&map[i][k]);
			}
		}

		ans = 0;

		for(int j = 0; j < H; j++){
			for(int k = 0; k < W; k++){
				if(map[j][k]){
					ans++;
					search_island(j,k);
				}
			}
		}

		printf("%d\n",ans);
		scanf("%d %d",&W,&H);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#include <cstdint>
#include <sys/time.h>

typedef std::int_fast32_t  s32;
typedef std::uint_fast32_t u32;
typedef std::int_fast64_t  s64;
typedef std::uint_fast64_t u64;

typedef std::pair<int, int> P;

int map[64][64];
int H, W;

void dfs_erase(int x, int y) {
  if( not ( 0 <= x and x < H and 0 <= y and y < W ) ) return;
  if( not ( map[x][y] == 1 ) ) return;

  map[x][y] = 0;
  
  int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
  for(int i = 0; i < 8; ++i) {
    dfs_erase(x + dx[i], y + dy[i]);
  }
  return;
}
  
int main(void) {

  for(;;) {
    std::cin >> W >> H;
    if( W == 0 and H == 0 ) break;

    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        std::cin >> map[i][j];
      }
    }

    int res = 0;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        if( map[i][j] == 1 ) {
          dfs_erase(i, j);
          res += 1;
        }
      }
    }

    std::cout << res << std::endl;
  }
  
  return 0;
}