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 w, h;
int field[60][60];

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

    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            int dx = x + i, dy = y + j;
            if (dx < 0 || dx >= h || dy < 0 || dy >= w) continue;
            if (field[dx][dy] == 0) continue;
            dfs(dx, dy);
        }
    }
}

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

        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (field[i][j] == 0) continue;
                dfs(i, j);
                cnt++;
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>  
#define rep(i, n) for(int i=0; i<(int)(n); ++i)
#define cout(val) cout << (val) << endl
using namespace std;
typedef long long LL;

int main(){
    int w, h, t0, t1, result, temp;
    pair<int, int> cur;
    vector<int> dx = {1, 1, 1, 0, 0, -1, -1, -1};
    vector<int> dy = {1, 0, -1, 1, -1, 1, 0, -1};
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector<LL> c(h);
        vector<LL> seen(h);
        vector<pair<int, int>> f;
        rep(i, h) rep(j, w){
            cin >> temp;
            c[i] |= (LL)temp<<j;
            if(temp) f.push_back(make_pair(i, j));
        }
        result = 0;
        stack<pair<int, int>> stk;
        for(auto &&pos : f){
            if(!(seen[pos.first]&1LL<<pos.second)) ++result;
            stk.push(make_pair(pos.first, pos.second));
            while(!stk.empty()){
                cur = stk.top();
                stk.pop();
                rep(i, 8){
                    t0 = cur.first + dy[i];
                    t1 = cur.second + dx[i];
                    if(t0==-1 || t0==h || t1==-1 || t1==w || seen[t0]&1LL<<t1 || !(c[t0]&1LL<<t1)) continue;
                    stk.push(make_pair(t0, t1));
                    seen[t0] |= 1LL<<t1;
                }
            }
        }
        cout(result);
    }
}

C++: #include <iostream>
using namespace std;
void dfs(int,int);
int data[52][52];
int main(){
  int w,h,i,j,cnt;
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(j=0;j<=w+1;j++){
	data[i][j]=0;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	cin >> data[i][j];
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
        if(data[i][j]==1){
	  dfs(i,j);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void dfs(int i,int j){
  if(data[i][j]==0) return ;
  data[i][j]=0;
  dfs(i-1,j);
  dfs(i+1,j);
  dfs(i,j-1);
  dfs(i,j+1);
  dfs(i-1,j-1);
  dfs(i-1,j+1);
  dfs(i+1,j-1);
  dfs(i+1,j+1);
}

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

int c[50][50];
int w, h;

void dfs(int x, int y) {
  c[y][x] = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i;
      int ny = y + j;
      if (c[ny][nx] == 1 && 0 <= nx && nx < w && 0 <= ny && ny < h)
	dfs(nx, ny);
    }
  }
}

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

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) return 0;

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

    int ans = search();
    cout << ans << endl;
  }
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#define INF 100000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

#define MAX 5000

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


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


int main(){
    for(;;){
        cin >> w >> h;
        if(w==0 and h == 0) break;
        int ans=0;
        for (int i=0; i<=h; i++) {
            for (int j=0; j<=w; j++) {
                G[i][j]=0;
            }
        }
        for (int i = 0; i<h;i++) {
            for (int j=0; j < w; j++) {
                cin >> G[i][j];
            }
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                if (G[i][j]==1) {
                    G[i][j]=0;
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout << ans <<endl;
    }
    return 0;
}