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;
using ll = long long int;

vector<pair<int, int>> dirs = {
    {-1, -1}, {-1, -0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, -0}, {1, 1},
};

void dfs(vector<vector<int>> &field, vector<vector<bool>> &visit, int x, int y,
         function<bool(int, int)> check) {
  visit[y][x] = true;

  for (const auto &dir : dirs) {
    int nx = x + dir.first;
    int ny = y + dir.second;
    if (check(nx, ny))
      dfs(field, visit, nx, ny, check);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;
    vector<vector<int>> field(h, vector<int>(w));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> field[y][x];
      }
    }
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    auto check = [&](int x, int y) {
      return (0 <= x && x < w) && (0 <= y && y < h) && field[y][x] == 1 &&
             !visit[y][x];
    };
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (visit[y][x] || field[y][x] == 0)
          continue;
        ans++;
        dfs(field, visit, x, y, check);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

vector<vector<int>> c(50, vector<int>(50));

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

vector<vector<bool>> seen(50, vector<bool>(50));
void dfs(vector<vector<int>> &c, int sy, int sx, int h, int w) {
    seen[sy][sx] = true;
    for(int i = 0; i < 8; i++){
        int ey = sy + dy[i], ex = sx + dx[i]; 
        if(ey < 0 || ey >= h || ex < 0 || ex >= w) continue;
        if(c[ey][ex] == 0 || seen[ey][ex]) continue;
        dfs(c, ey, ex, h, w);
    }
}

int main(){
    int w,  h;
    vector<int> cnt;
    int check = -1;
    cin >> w >> h;
    while (w != 0 || h != 0)
    {
        for(int i = 0 ; i < h; i++){
            for (int j = 0; j < w; j++)
            {
               cin >> c[i][j];
               seen[i][j] = false;
            }
        }
        cnt.push_back(0);
        check++;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if(!seen[i][j] && c[i][j] == 1){
                    dfs(c, i, j, h, w);
                    cnt[check]++;
                }
            }
        }
        cin >> w >> h;
    }
    for (int i = 0; i < cnt.size(); i++)
    {
        cout << cnt[i] << endl;
    }
    
}

C++: #include<iostream>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
const int WMAX = 100, HMAX = 100;
int w, h;
int island[WMAX][HMAX];

void dfs(int x, int y) {
  if (x < 0 or w <= x) return;
  if (y < 0 or h <= y) return;
//  cout << "(" << x << " " << y << ")" << endl;
  if (!island[x][y]) return;
  island[x][y] = false;
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
      dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  REP(dir, 8) {
    dfs(x + dx[dir], y + dy[dir]);
  }
}

int solve() {
  int ans = 0;
  REP(y, h) {
    REP(x, w) {
      if (island[x][y]) {
        ans++;
        dfs(x, y);
      }
    }
  }
  return ans;
}

int main() {
  while (cin >> w >> h and w) {
    REP(y, h) {
      REP(x, w) {
        int c;
        cin >> c;
        island[x][y] = c;
      }
    }

    cout << solve() << endl;
  }
}