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 _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll,ll> pll;
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
  return x+y-max(x,y);
}

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


void dfs(vector<vector<int>> &graph,int x,int y,vector<vector<bool>> &seen,int h,int w){
  seen[x][y]=true;
  rep(i,8){
    rep(j,8){
      int ax=x+dx[i];
      int ay=y+dy[i];
      if(ax<0||ay<0||ax>=h||ay>=w)continue;
      if(seen[ax][ay])continue;
      if(graph[ax][ay]==0)continue;
      dfs(graph,ax,ay,seen,h,w);
    }
  }
}

int main() {
rep(aaa,100000000){
  int h,w;cin >> w >> h;
  if(h==0&&w==0)return 0;
  vector<vector<bool>> seen(h,vector<bool>(w,false));
  vector<vector<int>> graph(h,vector<int>(w));
  rep(i,h)rep(j,w)cin >> graph[i][j];
  int ans=0;
  rep(v,h){
    rep(u,w){
      if(seen[v][u])continue;
      if(graph[v][u]==0)continue;
      dfs(graph,v,u,seen,h,w);
      ans++;
    }
  }
  cout << ans << endl;
}
}

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

typedef pair<int, int> P;

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

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

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>

using namespace std;
int w, h;
int c[100][100];
int dx[8]={-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8]={0, -1, 0, 1, -1, 1, -1, 1};
int cnt=0;

void search(int x, int y);

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

void search(int x, int y)
{
	int nx, ny;
	for(int i = 0; i < 8; i++){
		nx = x + dx[i];
		ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
		if(c[nx][ny] == 1){
			c[nx][ny] = 0;
			search(nx, ny);
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(obj) (obj).begin(),(obj).end()
typedef long long llong;
llong mod=1e9+7;

int w,h,cnt;
int a[50][50];
void dfs(int i,int j){
  if(a[i][j]==0) return;
  if(i<0||i>=w||j<0||j>=h) return;
  a[i][j]=0;
  dfs(i-1,j-1);
  dfs(i-1,j);
  dfs(i-1,j+1);
  dfs(i,j-1);
  dfs(i,j+1);
  dfs(i+1,j-1);
  dfs(i+1,j);
  dfs(i+1,j+1);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(1){
    cnt=0;
    cin>>h;
    if(h=='\0') break;
    cin>>w;
    rep(i,w){
      rep(j,h){
        cin>>a[i][j];
      }
    }
    rep(i,w){
      rep(j,h){
        if(a[i][j]==1){
          dfs(i,j);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }

  return 0;
  }

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void dfs(int x, int y)
{
    m[y][x] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
        else if (m[ny][nx] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        int isl = 0;

        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                cin >> m[i][j];

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

        cout << isl << endl;
    }

    return 0;
}