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 w,h;
int c[100][100];

int qx[1000000];
int qy[1000000];
int X,Y,left,right;

void push(int x,int y){
  right++;
  qx[right]=x;
  qy[right]=y;
}

void pop(){
  X=qx[left];
  Y=qy[left];
  left++;
}

void bfs(int sx,int sy){
  left=0;
  right=-1;
  push(sx,sy);
  c[sx][sy]=0;
  while(left<=right){
    pop();
    for(int nx=X-1;nx<=X+1;nx++){
      for(int ny=Y-1;ny<=Y+1;ny++){
        if(nx>=0 && ny>=0 && c[nx][ny]!=0){
          c[nx][ny]=0;
          push(nx,ny);
        }
      }
    }
  }
}

int main(){

  while(1){
    scanf("%d %d",&w,&h);
    if(w==0&&h==0)break;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }

    int ans=0;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(c[i][j]!=0){
          ans++;
          bfs(i,j);
        }
      }
    }

    printf("%d\n",ans);
    
  }
  return 0;
}

C++: #include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<utility>//pair
#include<list>
#include<vector>//可変長配列
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int,int>
#define PP pair<P,int>
#define F first
#define S second
int dx[8] = { 1,0,-1,0,1,-1,-1,1 };
int dy[8] = { 0,1,0,-1,1,1,-1,-1 };


int w, h;
int t[100][100];
bool visit[100][100];

void dfs(int ny, int nx) {
    if (visit[ny][nx])
        return;
    visit[ny][nx] = 1;

    rep(i, 8) {
        int x = nx + dx[i], y = ny + dy[i];
        if (x < 0 || y < 0 || x >= w || y >= h)
            continue;

        if(t[y][x]==1)
            dfs(y, x);
    }
}
int main() {
    while (cin >> w >> h, w || h) {
        rep(i, h)rep(j, w)cin >> t[i][j];
        rep(i, h)rep(j, w)visit[i][j] = 0;
        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (!visit[i][j] && t[i][j] == 1) {
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int n;
vector<vector<int> > C;
vector<vector<int> > D;

void bfs(int initCX, int initCY){
    queue<int> Q;
    Q.push(initCX);
    Q.push(initCY);
    D[initCX][initCY] = 0; // ?????????
    while(!Q.empty()){
        // ??????????´???????????????????
        int cx, cy;
        cx = Q.front();
        Q.pop();
        cy = Q.front();
        Q.pop();
        for(int i = -1; i <= 1; ++i){
            for(int j = -1; j <= 1; ++j){ // ??????????????????????????????
                if(C[cx + i][cy + j] == 1 && D[cx + i][cy + j] == -1){ // ???????????£??????????¨??????????
                    D[cx + i][cy + j] = D[cx][cy] + 1;
                    // ????????????????¨??????????????????????
                    Q.push(cx + i);
                    Q.push(cy + j);
                }
            }
        }
    }
}

int main(){
    int w, h;
    // index?????????????°???????????????£????????§???????????§h??¨w?????\?????????????????????????????????????????????????????????
    while(cin >> h >> w, w != 0 || h != 0){
        C = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                cin >> C[i][j];
            }
        }
        D = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                D[i][j] = -1;
            }
        }
        int n = 0;
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                if(C[i][j] == 1 && D[i][j] == -1){ // ???????????£??????????¨??????????
                    bfs(i, j);
                    ++n;
                }
            }
        }
        cout << n << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int w, int h){
	field[h][w] = 0;

	for(int nh=h-1;nh<h+2;nh++){
	    for(int nw=w-1;nw<w+2;nw++){
	      if(field[nh][nw]==1&&0<=nh&&0<=nw&&nh<h&&nw<w)dfs(nw,nh);
	    }
	  }
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			break;
		}

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					count++;
					dfs(j,i);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}