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++: #include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;

int n, m;
int field[51][51];

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

void dfs(int y, int x){
    field[y][x] = 0;
    rep(i,8){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || n < ny || nx < 0 || m < nx) continue;
        if(field[ny][nx] == 0) continue;
        dfs(ny, nx);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin >> m >> n;
        if(m == 0 && n == 0) break;
        rep(i,n)rep(j,m) cin >> field[i][j];
        int ans = 0;
        rep(i,n)rep(j,m){
            if(field[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

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

int C[55][55]={0};
int W,H;

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

void dfs(int h,int w){
  C[h][w]=0;
  
  rep(i,8){
    int nh=h+dy[i];
    int nw=w+dx[i];
    
    if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
    if(C[nh][nw]==0) continue;
    
    dfs(nh,nw);
  }
}

int main(){
  
  while(cin>>W>>H){
    if(W==0 && H==0) return 0;
    
    int cnt=0;
    rep(i,H){
      rep(j,W) cin>>C[i][j];
    }
    
    rep(i,H){
      rep(j,W){
        if(C[i][j]==0) continue;
        dfs(i,j);
        cnt++;
      }
    }
    
    cout<<cnt<<endl;
  }
  
  return 0;
}

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

vector<vector<int>> field(100, vector<int>(100));
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
void dfs(int y, int x) {
    field[y][x] = 0;
    for (int i = 0; i < 8; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (!field[ny][nx]) continue;
        if (field[ny][nx]) dfs(ny, nx);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int W, H;
    while (1) {
        cin >> W >> H;
        if (!W & !H) break;
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                cin >> field[i][j];
            }
        }
        int cnt = 0;
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                if (field[i][j] == 1) {
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <string>
#include <climits>

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define PRINT(STR) cout << (STR) << endl

typedef long long ll;
using namespace std;

const int SEA = 0;
const int GROUND = 1;

int w, h;
int board[51][51];

void dfs(int y, int x) {
	if (x < 0 || x >= w) return;
	if (y < 0 || y >= h) return;
	if (board[y][x] == 0) return;
	board[y][x] = 0;
	dfs(y - 1, x);
	dfs(y - 1, x + 1);
	dfs(y, x + 1);
	dfs(y + 1, x + 1);
	dfs(y + 1, x);
	dfs(y + 1, x - 1);
	dfs(y, x - 1);
	dfs(y - 1, x - 1);
}

void calc() {
	int count = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (board[i][j] == GROUND) {
				count++;
				dfs(i, j);
			}
		}
	}
	PRINT(count);
}

int main() {
	cin.tie(0); //対話型には使えない
	ios::sync_with_stdio(false); //printfと共には使えない
	while (cin >> w >> h, w + h != 0) {
		REP(i, h) {
			REP(j, w) {
				char c;
				cin >> c;
				if(c == '0') {
					board[i][j] = SEA;
				} else {
					board[i][j] = GROUND;
				}
			}
		}
		calc();
	}
	return 0;
}

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

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

int map[51][51];
int w, h;

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (x < 0 || w <= x) continue;
		if (y < 0 || w <= y) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}

C++: #include <iostream>

using namespace std;

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

// 0: 海, 1: 陸
int board[50][50] = {{0}};

void DFS(int i, int j, int w, int h) {
  if (board[i][j] == 0) return;
  board[i][j] = 0;  // mark as visited

  for (int d = 0; d < 8; ++d) {
    const int ny = i + dy[d];
    const int nx = j + dx[d];
    if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
      DFS(ny, nx, w, h);
    }
  }
}

int main() {
  int w, h;

  for (;;) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> board[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (board[i][j] == 1) {
          DFS(i, j, w, h);
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}