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 <queue>
#include <map>

using namespace std;

typedef pair<int, int> P;

int main() {
    int w, h;
    int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
    int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
    int map[50][50];
    while (true) {
        int count = 0;
        cin >> w >> h;
        if (w == 0) {
            return 0;
        }
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                cin >> map[x][y];
            }
        }
        for (int y0=0; y0<h; y0++) {
            for (int x0=0; x0<w; x0++) {
                queue<P> q;
                if (map[x0][y0] == 0) {
                    continue;
                }
                count++;
                q.push(P(x0, y0));
                while (!q.empty()) {
                    int x = q.front().first;
                    int y = q.front().second;
                    q.pop();
                    for (int i=0; i<8; i++) {
                        int nx = x+dx[i];
                        int ny = y+dy[i];
                        if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
                            continue;
                        }
                        if (map[nx][ny] == 0) {
                            continue;
                        }
                        map[nx][ny] = 0;
                        q.push(P(nx, ny));
                    }
                }
            }
        }
        cout << count << endl;
    }
}

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

int w,h;
int maze[100][100];

void solve(int x,int y){

  if(x < 0 || y < 0 || x == w || y == h) return;
  if(!maze[y][x]) return;

  maze[y][x] = 0;

  for(int i=-1;i<2;i++){
    for(int j=-1;j<2;j++){
      solve(x+i,y+j);
    }
  }

}
int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

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

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

    cout << ans << endl;

  }
}

C++: // include
#include <iostream>

// typedef
typedef unsigned long long ull;
typedef unsigned int uint;

// macro
#define REP(index, num) for (auto index = 0; index < num; index++)
#define REP1(index, num) for (auto index = 1; index <= num; index++)

#define MAX_NUM 60

// using namespace 
using namespace std;

const int ExistIsland(const int inX, const int inY);

// global
int W, H;
int c[MAX_NUM][MAX_NUM] = { 0 };

int main()
{
  while (1)
  {
    cin >> W >> H;

    // 入力が 0 0 なら終了
    if (W * H == 0)
      break;

    REP(hId, H)
      REP(wId, W)
      cin >> c[wId][hId];

    // 深さ優先探索
    int count = 0;
    REP(hId, H)
      REP(wId, W)
      count += ExistIsland(wId, hId);

    cout << count << endl;
  } 
}

const int ExistIsland(const int inX, const int inY)
{
  if (inX < 0 || W <= inX) return 0;
  if (inY < 0 || H <= inY) return 0;

  if (c[inX][inY] == 0)
    return 0;
  else
    c[inX][inY] = 0;

  //bool existFlag = true;

  // 周りに移動
  ExistIsland(inX + 1, inY);
  ExistIsland(inX + 1, inY + 1);
  ExistIsland(inX, inY + 1);
  ExistIsland(inX - 1, inY + 1);
  ExistIsland(inX - 1, inY);
  ExistIsland(inX - 1, inY - 1);
  ExistIsland(inX, inY - 1);
  ExistIsland(inX + 1, inY - 1);

  return 1;
}

C++: #include<iostream>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}