Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
	}
}

C++: #ifndef _WIN32
#include<iostream>
#endif

using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

typedef vector<int> V;
typedef vector<V> VV;

VV wrap(VV vs,int c) {
	int R = vs.size();
	int C = vs[0].size();
	VV ret(R + 2, V(C + 2, c));
	REP(i, R)REP(j, C)ret[i + 1][j + 1] = vs[i][j];
	return ret;
}
const int dr[] = { 0,0,-1,1 ,1,1,-1,-1};
const int dc[] = { -1,1,0,0, -1,1,-1,1};
VV g;
void dfs(int r,int c) {
	if (g[r][c] == 0)return;
	//cout << "(" << r << "," << c << ")";
	g[r][c] = 0;
	REP(i, 8) {
		int nr = r + dr[i];
		int nc = c + dc[i];
		dfs(nr, nc);
	}
}
int main() {
	int R, C;
	while (cin >> C >> R, C + R) {
		VV in(R,V(C));
		REP(i, R)REP(j,C)cin >> in[i][j];
		g = wrap(in,0);
		int res = 0;
		REP(i, R)REP(j, C) {
			if (g[i + 1][j + 1]) {
				res++;
				dfs(i + 1, j + 1);// cout << endl;
			}
		}

		cout << res << endl;
	}


}

C++: #define rep(i, n) for(int i = 0; i < n; i++)
#define ck(a, b) (0<=a && a<b)
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1}, dy[]={-1,-1,-1,0,0,1,1,1};
int w, h; int il[50][50];

int main(){
	while(cin>>w>>h, w||h){
		int c = 0;
		rep(y, h)rep(x, w)cin>>il[y][x];
		rep(y, h)rep(x, w){
			if(il[y][x]){
				int cked[50][50] = {0};
				cked[y][x] = 1;
				bool finish = 0;
				while(!finish){
					finish = 1;
					rep(i, h)rep(j, w){
						if(cked[i][j]){
							rep(d, 8){
								if(ck(j+dx[d],w) && ck(i+dy[d],h) &&
									il[i+dy[d]][j+dx[d]] &&
									!cked[i+dy[d]][j+dx[d]]
								){
									cked[i+dy[d]][j+dx[d]] = 1; finish = 0;
								}
							}
						}
					}
				}
				c++;
				rep(i, h)rep(j, w)if(cked[i][j])il[i][j]=0;
			}
		}
		cout<<c<<endl;
	}
	return 0;
}

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

int map[50][50];
int w,h,label;

void check(int x,int y,int lnum){
  map[y][x]=lnum;
  if (x>0){
    if (map[y][x-1]==1) check(x-1,y,lnum);
    if (y>0){
      if (map[y-1][x-1]==1) check(x-1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x-1]==1) check(x-1,y+1,lnum);
    }
  }
  if(x<w-1){
    if (map[y][x+1]==1) check(x+1,y,lnum);
    if (y>0){
      if (map[y-1][x+1]==1) check(x+1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x+1]==1) check(x+1,y+1,lnum);
    }
  }
  if (y>0){
    if (map[y-1][x]==1) check(x,y-1,lnum);
  }
  if (y<h-1){
    if (map[y+1][x]==1) check(x,y+1,lnum);
  }
}

int main(){
  cin >> w >> h;
  while (w!=0||h!=0){
    label=2;
    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++){
	if (map[i][j]==1){
	  //cout << "(" << map[i][j] << ")";
	  check(j,i,label++);
	}
      }
    }
    //cout << "(" << label << ")";

    cout << (label-2) << endl;

    cin >> w >> h;
 }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;
int field[50][50];
int w, h;

void dfs(int i, int j){
    field[i][j] = 0;
    for(int di=-1; di<=1; di++){
        for(int dj=-1; dj<=1; dj++){
            int ni = i+di;
            int nj = j+dj;
            if(field[ni][nj]==1 && 0 <= ni && ni < h &&
                    0 <= nj && nj < w){
                dfs(ni,nj);
            }
        }
    }
    return;
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        REP(i,h){
            REP(j,w){
                cin >> field[i][j];
            }
        }

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

    return 0;
}