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>
#define INF 1000000000
using namespace std;

vector<pair<int, int>> bfs(vector<pair<int, int>> not_yet, vector<vector<int>> maze)
{
  pair<int, int> check = not_yet.at(0);
  queue<pair<int, int>> a;
  a.push(check);
  maze.at(check.first).at(check.second) = 0;
  while (!a.empty())
  {
    pair<int, int> now = a.front();
    a.pop();
    not_yet.erase(find(not_yet.begin(), not_yet.end(), now));
    vector<int> move = {1, 0, -1};
    for (int i = 0; i < 3; ++i)
    {
      for (int j = 0; j < 3; ++j)
      {
        if (i == 1 && j == 1)
        {
          continue;
        }
        if (maze.at(now.first + move.at(i)).at(now.second + move.at(j)) == 1)
        {
          a.push({now.first + move.at(i), now.second + move.at(j)});
          maze.at(now.first + move.at(i)).at(now.second + move.at(j)) = 0;
        }
      }
    }
  }
  return not_yet;
}

int main()
{
  while (true)
  {
    int w, h;
    cin >> w >> h;
    if (w == h && w == 0)
    {
      break;
    }
    vector<vector<int>> maze(h + 2, vector<int>(w + 2, -1));
    vector<pair<int, int>> not_yet(0);// h w
    for (int i = 1; i < h + 1; ++i)
    {
      for (int j = 1; j < w + 1; ++j)
      {
        int point;
        cin >> point;
        maze.at(i).at(j) = point;
        if (point == 1)
        {
          not_yet.push_back({i, j});
        }
      }
    }

    int ans = 0;
    while (not_yet.size() != 0)
    {
      not_yet = bfs(not_yet, maze);
      ans += 1;
    }
    cout << ans << endl;
  }
}

C++: #include <stdio.h>
#include <iostream>

using namespace std;

int c[51][51];

void deep(int i,int n){
  if(c[i][n+1]==1){
    c[i][n+1]=2;
    deep(i,n+1);
  }
  if(c[i+1][n]==1){
    c[i+1][n]=2;
    deep(i+1,n);
  }
  if(c[i][n-1]==1){
    c[i][n-1]=2;
    deep(i,n-1);
  }
  if(c[i-1][n]==1){
    c[i-1][n]=2;
    deep(i-1,n);
  }
  if(c[i+1][n+1]==1){
    c[i+1][n+1]=2;
    deep(i+1,n+1);
  }
  if(c[i+1][n-1]==1){
    c[i+1][n-1]=2;
    deep(i+1,n-1);
  }
  if(c[i-1][n+1]==1){
    c[i-1][n+1]=2;
    deep(i-1,n+1);
  }
  if(c[i-1][n-1]==1){
    c[i-1][n-1]=2;
    deep(i-1,n-1);
  }
}

int main(){
  while(1){
    for(int i=0;i<51;i++){
      for(int n=0;n<51;n++){
          c[i][n]=0;
      }
    }
    int w,h;
    int num=0;

    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          scanf("%d",&c[i][n]);
      }
    }

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          if(c[i][n]==1){
            num++;
            deep(i,n);
          }
      }
    }

    printf("%d\n",num);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<vector>
using namespace std;
typedef pair<int, int>P;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
//max=({});
//条件式が真ならwhileの中身を回し続ける
//printf("%d\n", ans);

//pairの入力
//vector<pair<ll, ll>>work(n);
//rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
//for(auto p:mp)(mapの探索)
//printf("%.10f\n",なんちゃら)
//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//素因数分解
map<ll, ll>ins;
void facterize(ll n) {
	ll a = 2;
	while (n >= a * a) {
		if (n % a == 0) {
			ins[a]++;
			ins[a] = ins[a] % 1000000007;
			n /= a;
		}
		else {
			a++;
		}
	}
	ins[n]++;
}

int keta(int x) {
	int y = 0;
	while (x > 0) {
		y++;
		x /= 10;
	}
	return y;
}

ll dx[8] = { 0,0,-1,1 };
ll dy[8] = { -1,1,0,0 };
ll dp[1010][1010];
ll land[101][101];
bool flag[101][101];
ll h, w;

void dfs(ll y, ll x) {
	flag[y][x] = false;
	for (ll dx = -1;dx <= 1;++dx) {
		for (ll dy = -1;dy <= 1;++dy) {
			if (0 <= y + dy && y + dy < h && 0 <= x + dx && x + dx < w && flag[y + dy][x + dx]) {
				dfs(y + dy, x + dx);
			}
		}
	}
	return;
}

//long longしか使わない
int main() {
	while (cin >> w >> h,w||h) {
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				cin >> land[i][j];
				if (land[i][j])flag[i][j] = true;
			}
		}
		ll ans = 0;
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				if (flag[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}