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>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = INT_MAX;
const ll LLINF = INT64_MAX;
const double PI = acos(-1);
const int MOD = 1000000007;

//-------------------------------------

using P = pair<int, int>;

void dfs(int x, int y, int h, int w, vector<vector<int>> &g, vector<vector<bool>> &seen)
{
    stack<P> st;
    st.push(P(x, y));
    while (st.size())
    {
        P now = st.top();
        st.pop();
        seen[now.second][now.first] = true;
        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                int nx = now.first + dx;
                int ny = now.second + dy;
                if (0 <= nx && nx < w && 0 <= ny && ny < h && !seen[ny][nx] && g[ny][nx] == 1)
                {
                    st.push(P(nx, ny));
                }
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true)
    {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0)
        {
            return 0;
        }
        // 陸は1、海は0
        vector<vector<int>> g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                cin >> g[i][j];
            }
        }
        vector<vector<bool>> seen(h, vector<bool>(w, false));
        int ans = 0;
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (!seen[y][x] && g[y][x] == 1)
                {
                    dfs(x, y, h, w, g, seen);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

int c[60][60];
bool visited[60][60];
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int h, w;

bool check(int x, int y){
  if(x < 0 || y < 0)return false;
  if(x >= w || y >= h)return false;
  if(visited[y][x])return false;
  if(c[y][x] == 0)return false;
  return true;
}

void dfs(int x, int y){ 
  if(!check(x, y))return;

  visited[y][x] = true;
  for(int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i]);
  }
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        scanf("%d", &c[i][j]);
      }
    }

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

    int cnt = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!visited[i][j] && c[i][j] == 1){
          dfs(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>

using namespace std;

#define WH 52

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

void dfs(int,int);

int main(int argc, char const *argv[])
{
	int w,h;


	int cnt;

	while(1){
		cin>>w>>h;
		if(w+h==0) break;

		for(int i1=0;i1<WH;i1++){
			for(int i2=0;i2<WH;i2++){
				f[i1][i2]=0;
			}
		}

		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				cin>>f[i1][i2];
			}
		}

		cnt=0;
		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				if(f[i1][i2]==1){
					f[i1][i2]=0;
					dfs(i2,i1);
					cnt++;
				}
			}
		}

		cout<<cnt<<endl;

	}

	return 0;
}

void dfs(int x,int y){
	for(int i1=0;i1<8;i1++){
		for(int i2=0;i2<8;i2++){
			if(f[y+dy[i1]][x+dx[i2]]==1){
				f[y+dy[i1]][x+dx[i2]]=0;
				dfs(x+dx[i2],y+dy[i1]);
			}
		}
	}
}