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++: #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace::std;
#define rep(i, n) for(lint i = 0; i < (lint)(n); i++)
#define repi(i,a,b) for(lint i=lint(a);i<lint(b);i++)
typedef long long lint;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<typename T=lint> T in(){
    return *istream_iterator<T>(cin);
}

vector<lint> dx={-1,1,0,0,1,1,-1,-1};
vector<lint> dy={0,0,-1,1,1,-1,1,-1};
int main(){
    while(1){
        lint w,h;
        cin>>w>>h;
        if(w==0&&h==0)break;
        vector<vector<lint>>v(h,vector<lint>(w));
        rep(i,h)rep(j,w){
            cin>>v[i][j];
        }
        lint ans=0;
        rep(i,h)rep(j,w){
            if(v[i][j]==0)continue;
            ans++;
            stack<pair<lint,lint>>s;
            s.emplace(i,j);
            while(!s.empty()){
                lint r,c;
                tie(r,c)=s.top();
                s.pop();
                rep(k,8){
                    if(r+dy[k]<0||h<=r+dy[k]||c+dx[k]<0||w<=c+dx[k])continue;
                    if(v[r+dy[k]][c+dx[k]]==1){
                        v[r+dy[k]][c+dx[k]]=0;
                        s.emplace(r+dy[k],c+dx[k]);
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define unsigned long long 
#define rep(i,a,b) for(ull (i)=(a);(i)<(b);i++)
void dfs(int w, int h);
using namespace std;
int field[50][50];
int x, y;
int cnt;
int main(void){
	while (true){
		cin >> x >> y;
		if (x == 0)
			break;
		cnt = 0;
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				cin >> field[i][j];
			}
		}
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				if (field[i][j] == 1){
					cnt++;
					dfs(i, j);
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}


void dfs(int h, int w){
	field[h][w] = 0;

	if (w < x - 1){
		if (field[h][w + 1] == 1){
			dfs(h, w + 1);
		}
	}
	if (w < x - 1&&h<y-1){
		if (field[h+1][w + 1] == 1){
			dfs(h+1 ,w + 1);
		}
	}
	if (h<y - 1){
		if (field[h+ 1][w ] == 1){
			dfs(h +1 ,w );
		}
	}
	if (h<y - 1&&w>0){
		if (field[h + 1][w-1] == 1){
			dfs(h + 1, w-1);
		}
	}
	if (w>0){
		if (field[h ][w - 1] == 1){
			dfs(h , w - 1);
		}
	}
	if (w>0&&h>0){
		if (field[h-1][w - 1] == 1){
			dfs(h-1, w - 1);
		}
	}
	if (h>0){
		if (field[h - 1][w] == 1){
			dfs(h - 1, w);
		}
	}
	if (h>0&&w<x-1){
		if (field[h - 1][w+1] == 1){
			dfs(h - 1, w+1);
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

vector<int> dx = {1, 1, 1, -1, -1, -1, 0, 0}, dy = {1, -1, 0, 1, -1, 0, 1, -1};
int w, h;
vector<vector<bool>> field;

void dfs(int x, int y)
{
  field.at(y).at(x) = false;
  rep(direction, 8)
  {
    int nx = x + dx.at(direction), ny = y + dy.at(direction);
    if (0 <= nx && nx < w && 0 <= ny && ny < h && field.at(ny).at(nx))
    {
      dfs(nx, ny);
    }
  }
}

int main()
{
  while (1)
  {
    cin >> w >> h;
    if (!w && !h)
    {
      break;
    }
    field.resize(h, vector<bool>(w));
    field.assign(h, vector<bool>(w, false));
    rep(y, h)
    {
      rep(x, w)
      {
        int c;
        cin >> c;
        if (c)
        {
          field.at(y).at(x) = true;
        }
      }
    }
    int ans = 0;
    rep(y, h)
    {
      rep(x, w)
      {
        if (field.at(y).at(x))
        {
          dfs(x, y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}