Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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