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 <stack>
#include <queue>
#include <vector>
using namespace std;

int MAP(vector<vector<int> > &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int> > map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}

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

int land[50][50], flag[50][50];
int w, h;
int xroot[] = {0,0,1,1,1,-1,-1,-1};
int yroot[] = {1,-1,0,-1,1,0,1,-1};
int cnt = 1;


void dfs(int x, int y){


  if(land[y][x] != 1) return;
  land[y][x] = cnt;

  for(int i = 0; i < 8; i++){
    int nx = x + xroot[i], ny = y + yroot[i];
    if(ny >= h || nx >= w || ny < 0 || nx < 0) continue;
    // cout << nx << " " << ny << endl;
    dfs(nx,ny);
  }
}

int main(){

  while(cin >> w >> h && (w || h)){
    cnt = 1;
    memset(flag,0,sizeof(flag));

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



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

  }



  return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1e18;
long long mod=1e9+7;
int maze[55][55]={};
bool reached[55][55]={};
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
int w,h;
int islands;

void search(int nowy,int nowx,bool start){

    if(reached[nowy][nowx]||maze[nowy][nowx]==0)return;

    if(start)islands++;

    reached[nowy][nowx]=true;
    //cout<<nowy<<" "<<nowx<<" "<<endl;
    rep(i,8)
    {
        int nx=nowx+dx[i];
        int ny=nowy+dy[i];

        if(nx<1 || nx>w || ny<1 || ny>h||reached[ny][nx]|| !maze[ny][nx])continue;

        search(ny,nx,false);
    }
    //cout<<islands<<" end"<<endl;
    return;
}
int main(){
    

    while(1){
    
    cin>>w>>h;
    if(w==0 && h==0)break;
    //cout<<w<<" "<<h<<endl;
    islands=0;

    rep2(i,50)rep2(k,50){
        reached[i][k]=false;
        maze[i][k]=0;
    }
    rep2(i,h)rep2(k,w){
        int t;
        cin>>t;
        maze[i][k]=t;
    }

    rep2(i,h)rep2(k,w)search(i,k,true);

    cout<<islands<<endl;
    
    }
    return 0;

}
/*


*/

C++: #include <bits/stdc++.h>

using namespace std;

int w, h;

vector<vector<int>> makearea(void) {
	vector<vector<int>> area(h);

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			int tmp;
			cin >> tmp;
			area[i].push_back(tmp);
		}
	}
	
	/*for(auto i : area) {
		for(auto j : i) {
			cout << j << " ";
		}
		cout << endl;
	}*/

	return area;
}

void dfs(int y, int x, vector<vector<int>>& area, vector<vector<bool>>& checker) {
	if(x != w - 1) {
		if(area[y][x + 1] == 1 and checker[y][x + 1] == true) {
			checker[y][x + 1] = false;
			dfs(y, x + 1, area, checker);
		}
	}
	if(x != w - 1 and y != h - 1){
		if(area[y + 1][x + 1] == 1 and checker[y + 1][x + 1]) {
			checker[y + 1][x + 1] = false;
			dfs(y + 1, x + 1, area, checker);
		}
	}
	if(y != h - 1){
		if(area[y + 1][x] == 1 and checker[y + 1][x]) {
			checker[y + 1][x] = false;
			dfs(y + 1, x, area, checker);
		}
	}
	if(x != 0 and y != h - 1){
		if(area[y + 1][x - 1] == 1 and checker[y + 1][x - 1]) {
			checker[y + 1][x - 1] = false;
			dfs(y + 1, x - 1, area, checker);
		}
	}
	if(x != 0){
		if(area[y][x - 1] == 1 and checker[y][x - 1]) {
			checker[y][x - 1] = false;
			dfs(y, x - 1, area, checker);
		}
	}
	if(x != 0 and y != 0){
		if(area[y - 1][x - 1] == 1 and checker[y - 1][x - 1]) {
			checker[y - 1][x - 1] = false;
			dfs(y - 1, x - 1, area, checker);
		}
	}
	if(y != 0){
		if(area[y - 1][x] == 1 and checker[y - 1][x]) {
			checker[y - 1][x] = false;
			dfs(y - 1, x, area, checker);
		}
	}
	if(x != w - 1 and y != 0){
		if(area[y - 1][x + 1] == 1 and checker[y - 1][x + 1]) {
			checker[y - 1][x + 1] = false;
			dfs(y - 1, x + 1, area, checker);
		}
	}
}

int search(vector<vector<int>>& area) {
	int sum = 0;
	vector<vector<bool>> checker;
	checker = vector<vector<bool>>(area.size(), vector<bool>(area[0].size(), true));

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j ++) {
			if(area[i][j] == 1 and checker[i][j]) {
				checker[i][j] = false;
				dfs(i, j, area, checker);
				sum++;
			}
		}
	}
	return sum;
}

int main(void) {
	while(1) { 
		cin >> w >> h;
		if(w == 0 and h == 0) {
			break;
		}

		vector<vector<int>> area = makearea();
		int sum = search(area);

		cout << sum << endl;
	}
	return 0;
}