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 <iostream>
using namespace std;

int myMap[51][51];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < h && y < w);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

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 >> myMap[i][j];
	    }
	}

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1){
		    cout << i << " " << j << endl;
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
}

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