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++: /*
 * B.cpp
 *
 *  Created on: 2017/05/27
 *      Author: aihara
 */

#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout << "#x" << ": " << x << endl;
#else
#define dbp(x)
#endif
#define ll long long
using namespace std;

const int maxh = 60;
int w, h;
int c[maxh][maxh];
int di[9] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dj[9] = { -1, 0, 1, -1, 1, -1, 0, 1 };

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

int main() {
	while (cin >> w >> h, w || h) {
		ans = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> c[i][j];
			}
		}
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				ans += dfs(i, j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

void dfs(int h, int w, int(*A)[50], int W, int H) {
  A[h][w] = 0;
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      int hp = h + i;
      int wp = w + j;
      if (0 <= hp && hp < H && 0 <= wp && wp < W && A[hp][wp] == 1) {
        dfs(hp, wp, A, W, H);
      }
    }
  }
}

int main() {
  int A[50][50];
  int W, H, ans;

  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) break;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> A[i][j];
      }
    }

    ans = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (A[i][j] == 1) {
          dfs(i, j, A, W, H);
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }  

  return 0;
}

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

void trip(int y,int x,vector<vector<int>>&mp){
	mp.at(y).at(x)=0;
	for(int dy=-1;dy<=1;++dy){
		for(int dx=-1;dx<=1;++dx){
			int nxy=y+dy,nxx=x+dx;
			if(nxy<0||nxy>=mp.size()||nxx<0||nxx>=mp.at(nxy).size()||!mp.at(nxy).at(nxx))continue;
			trip(nxy,nxx,mp);
		}
	}
}

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	while(true){
		int w,h; cin>>w>>h;
		if(w==0&&h==0)break;
		vector<vector<int>>mp(h,vector<int>(w));
		for(auto&&i:mp)for(auto&&j:i)cin>>j;
		int cnt=0;
		for(int y=0;y<h;++y){
			for(int x=0;x<w;++x){
				if(mp.at(y).at(x)){
					trip(y,x,mp);
					++cnt;
				}
			}
		}
		cout<<cnt<<"\n"s;
	}
}

C++: //AOJ How many islands?
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)//repをdefineした際は=でつながないようにね
#define rep2(i,a,n) for(int i=a;i<=n;i++)
int H,W;
vector<vector<int>> area;

void dfs(int i,int j){
    area[i][j]=0; //0にしておくことで隣接したマス目の探索をしているときに再度探索されることを防ぐ
    rep2(di,-1,1) rep2(dj,-1,1) {
        int ni=i+di,nj=j+dj;
        if(ni<0||nj<0||ni>=H||nj>=W) continue;
        if(area[ni][nj]==0) continue;
       // cout<<ni<<" "<<nj<<endl;
        dfs(ni,nj);
    }    
    //帰りがけの処理をするならここで
    return;
}

int main() {

while(cin>>W>>H,H!=0&&W!=0){
    area.assign(H,vector<int>(W,0));//area 2次元配列の初期化はassignを使うとforを使った代入の必要がない 
    rep(i,0,H) rep(j,0,W) cin>>area[i][j]; 
    //探索開始
    int c=0;
    rep(i,0,H) rep(j,0,W) {
        if(area[i][j]==0) continue;
        dfs(i,j);
        c++;
    }
    cout<<c<<endl;
}
return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

int dx[8] = { 0,-1,-1,-1,0,1,1,1 }, dy[8] = { -1,-1,0,1,1,1,0,-1 };

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<0 || ny<0 || nx>w || ny>h)
			continue;
		if (c[ny][nx] == 1)
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				++sum;
				dfs(j, i);
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		int res = solve();
		std::cout << res << std::endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("B", "r", stdin);

typedef pair<int,int> P;

int n, m;
int cnt = 0;
int data[100][100];
int check[100][100];

int mvx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int mvy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void input() {
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++) {
			scanf("%d", &data[i][j]);
		}
	}
	cnt = 0;
	memset(check, 0, sizeof(check));
}

void bfs(int x, int y) {
	queue<P> que;
	que.push(P(x,y));
	check[x][y] = 1;
	while(!que.empty()){
		P cur = que.front(); que.pop();
		for(int i = 0; i < 8; i++) {
			if(cur.first + mvx[i] >= 0 && cur.first + mvx[i] < n && cur.second + mvy[i] >= 0 && cur.second + mvy[i] < m
				&& data[cur.first + mvx[i]][cur.second + mvy[i]] && !check[cur.first+mvx[i]][cur.second+mvy[i]]){
					check[cur.first +mvx[i]][cur.second+mvy[i]] = 1;
					que.push(P(cur.first+mvx[i], cur.second+mvy[i]));
			}
		}
	}
}

void process() {
	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			if(data[i][j] && !check[i][j]) {
				bfs(i,j);
				++cnt;
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	while(true) {
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		input();
		process();
	}
	return 0;
}