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 <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
#define MAX_N 50

int field[MAX_N][MAX_N];

void init(){
	rep(i,MAX_N){
		rep(j,MAX_N){
			field[i][j] = 0;
		}
	}
}

void rec(int x,int y,int maxW,int maxH){
	if(field[y][x] == 0){
		return;
	}
	field[y][x] = 0;
	int dx[8] = {-1,0,1,-1,1,-1,0,1};
	int dy[8] = {-1,-1,-1,0,0,1,1,1};
	rep(i,8){
		int nx,ny;
		nx = x + dx[i];
		ny = y + dy[i];
		if( 0 <= nx && nx < maxW && 0 <= ny && ny < maxH){
			rec(nx,ny,maxW,maxH);
		}
	}
	return;
}

int main()
{
	int w,h;
	cin>>w>>h;
	while( !((w == 0) && (h == 0))){
		init();
		rep(i,h){
			rep(j,w){
				cin>>field[i][j];
			}
		}
		int ans = 0;
		rep(i,h){
			rep(j,w){
				if(field[i][j] == 1){
					ans++;
					rec(j,i,w,h);
				}
			}
		}
		cout<<ans<<endl;
		cin>>w>>h;
	}

	return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						int tmp1,tmp2,tmp3,tmp4;
						tmp1=a[i][j-1];
						tmp2=a[i-1][j-1];
						tmp3=a[i-1][j];
						tmp4=a[i-1][j+1];
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp1)a[k][l]=tmp;}}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp2)a[k][l]=tmp;}}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp3)a[k][l]=tmp;}}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp4)a[k][l]=tmp;}}}
					}
				}
			}
		}
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}

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

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout << is << endl;
    cin >> w >> h;
  }
  return 0;
}

void dfs(int x,int y){
  int i = 0;
  map[x][y] = 0;
  for(i = 0;i < 8; i++){
    if(x + dx[i] > -1 && y + dy[i] > -1 && x + dx[i] < h && y + dy[i] < w){
      if(map[x + dx[i]][y + dy[i]] == 1){
	dfs(x + dx[i],y + dy[i]);
      }
    }
  }
}

C++: #include <iostream>

using namespace std;

const int MAX_LENGTH = 50;

int map[MAX_LENGTH][MAX_LENGTH];

void dfs(int x, int y) {
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (y + i >= 0 && MAX_LENGTH > y + i && x + j >= 0 && MAX_LENGTH > x + j && map[y+i][x+j] == 1) {
				map[y+i][x+j] = 0;
				dfs(x + j, y + i);
			}
		}
	}

	return;
}

int main() {
	while (true) {
		int w, h, count;
		cin >> w >> h;
		if (w == 0) {
			break;
		}
		for (int i = 0; i < MAX_LENGTH; i++) {
			for (int j = 0; j < MAX_LENGTH; j++) {
				map[i][j] = 0;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					dfs(j, i);
					count++;
					/*
					for (int k = 0; k < h; k++) {
						for (int l = 0; l < w; l++) {
							cout << map[k][l] << " ";
						}
						cout << endl;
					}
					cout << endl;
					*/
				}
			}
		}
		cout << count << endl;
	}

	return 0;
}