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>
int h, w;
void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(0 <= i && i < h && 0 <= j && j < w && a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

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

int H, W, C[50][50];

void dfs(int a, int b) {
    C[a][b] = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            int na = a + i, nb = b + j;
            if (na >= 0 && na < H && nb >= 0 && nb < W && C[na][nb] == 1) dfs(na, nb);
        }
    }
    return;
}

int main() {
    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 >> C[i][j];
        }

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

        cout << ans << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

int main(){
    while(1){
    int W,H;
    scanf("%d %d\n",&W,&H);
    if(W==0)break;
    int c[100][100];//W,H<=50
    bool f[100][100];
    rep(i,H){
        rep(j,W){
            scanf("%d",&c[i][j]);
            f[i][j]=false;
        }
    }
    queue< pair<int,int> > q;
    int count=0;
    rep(i,H)rep(j,W){
        if(f[i][j])continue;
        if(c[i][j]==0)continue;
        count++;
        //printf("x=%d y=%d",i,j);
        q.push( mp(i,j) );
        while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int py=p.first;int px=p.second;
            if(f[py][px])continue;
            if(c[py][px]==0)continue;
            f[py][px]=true;
            if(py>0)q.push( mp(py-1,px) );
            if(py<H-1)q.push( mp(py+1,px) );
            if(px>0)q.push( mp(py,px-1) );
            if(px<W-1)q.push( mp(py,px+1) );
            if(py>0&&px>0)q.push( mp(py-1,px-1));
            if(py>0&&px<W-1)q.push( mp(py-1,px+1));
            if(py<H-1&&px>0)q.push( mp(py+1,px-1));
            if(py<H-1&&px<W-1)q.push( mp(py+1,px+1));

        }
    }
    
    printf("%d\n",count);
    }
}

C++: #include <bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;

int dx8[8] = { -1,-1,-1, 0, 1, 1, 1, 0 };
int dy8[8] = { -1, 0, 1, 1, 1, 0,-1,-1 };

vector<vector<int>>as;
int w,h;

void check(int y, int x) {
    for (int i = 0; i < 8; ++i) {
        int nx = x + dx8[i];
        int ny = y + dy8[i];
        if (as[ny][nx]) {
            as[ny][nx] = 0;
            check(ny, nx);
        }
    }
}

int main() {
    while (1) {
        cin >> w >> h;
        if (!w)break;
        vector<vector<int>>as2(h+2);
        as = as2;
        for (int i = 0; i <= h+1; ++i) {
            as[i].emplace_back(0);
            if (i != 0 && i != h + 1) {
                for (int j = 0; j < w; ++j) {
                    int c; cin >> c;
                    as[i ].emplace_back(c);
                }
            }
            else {
                for (int j = 0; j < w; ++j) {
                    as[i].emplace_back(0);
                }
            }
            
            as[i].emplace_back(0);
        }
        int ans = 0;
        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                if (as[i][j]) {
                    ans++;
                    check(i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

int W, H;
int field[52][52];

void dfs(int x, int y) {
  field[y][x] = 0;

  REP(dx, -1, 2) REP(dy, -1, 2) {
    int tx = x + dx;
    int ty = y + dy;
    if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;
    if (field[ty][tx]) dfs(tx, ty);
  }
}

int main() {
  for(; cin >> W >> H, (W | H) != 0; ) {

    rep(i, H) rep(j, W) cin >> field[i][j];

    int ans = 0;
    rep(i, H) rep(j, W) if (field[i][j]) {
      dfs(j, i);
      ans++;
    }

    cout << ans << endl;
  }
  
  
  return 0;
}