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<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

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

using namespace std;


int read_map(vector< vector <int> >& map,int &w, int &h){
  cin >> w >>  h;
  if (w == 0 && h == 0){
    return 0;
  }
  map.reserve(h);
  for (int i = 0; i < h ; ++i) map[i].resize(w);
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      cin >> map[i][j];
    }
  }
  return 1;
}


bool island_check(vector<vector<int> >& map, int i, int j){
  if (map[i][j] == 1){
    return true;
  }
  return false;
}

void walk_island(vector<vector<int> >& map, int i, int j, int w, int h){
  int start_i, start_j;
  start_i = i;
  start_j = j;
  while (++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (++i < h){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (++i < h && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (++i < h && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
}

int search_island_num(vector< vector <int> >& map, int w, int h){
  int island_count = 0;
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      if(map[i][j] == 1){
        island_count++ ;
        map[i][j] = 2;
        walk_island(map, i, j, w, h);
      }
    }
  }
  return island_count;
}

int main(){
  vector< vector<int> > map;
  int w, h;
  while (read_map(map, w, h) != 0) {
    cout << search_island_num(map, w, h) << endl;
    map.clear();
  }
}

C++: #include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#define INF 1000000000ll
#define MOD 1000000007ll
#define EPS 1e-10
#define REP(i,m) for(long long i=0; i<m; i++)
#define FOR(i,n,m) for(long long i=n; i<m; i++)
#define DUMP(a) for(long long dump=0; dump<(ll)a.size(); dump++) { cout<<a[dump]; if(dump!=n-1) cout<<" "; else cout<<endl; }
#define ALL(v) v.begin(),v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

/*
 貪欲
 ?決定問題 必ず解に含まれる(含むように変換できる)ものから決めていく みんプロ本選B
 ?最適化問題 任意の解から評価を落とさず解に含められるようなものから決めていく 区間スケジューリングとか
 ?
 */

/*
 DP
 ?DPはある程度決め打ちをするべき(解を帰納的に構成できないか？)
 */

void dfs(ll i, ll j, vector<vector<int>>& c) {
	c[i][j]=0;
	for(ll dx=-1; dx<=1; dx++) {
		for(ll dy=-1; dy<=1; dy++) {
			if(i+dx>=0&&i+dx<(ll)c.size()&&j+dy>=0&&j+dy<=(ll)c[0].size()&&c[i+dx][j+dy]==1) dfs(i+dx,j+dy,c);
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(1) {
	ll w,h;
	cin>>w>>h;
		if(w==0&&h==0) break;
	vector<vector<int>> c(h,vector<int>(w));
	REP(i,h) REP(j,w) cin>>c[i][j];
	ll ret=0;
	REP(i,h) REP(j,w) {
		if(c[i][j]==1) {
			ret++;
			dfs(i,j,c);
		}
	}
	cout<<ret<<endl;
	}
}