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>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int w, h;
vector<vector<int>> m(50, vector<int>(50));
vector<vector<bool>> searched(50, vector<bool>(50, false));
vector<int> dx = {1, 1, 1, 0, -1, -1, -1, 0};
vector<int> dy = {-1, 0, 1, 1, 1, 0, -1, -1};

void DFS(int i, int j) {
    if (i < 0 || h <= i || j < 0 || w <= j) return;
    if (m[i][j] == 0) return;
    if (searched[i][j]) return;
    
    searched[i][j] = true;
    rep(k, 8) DFS(i + dx[k], j + dy[k]);
    return;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(j, w) cin >> m[i][j];
        rep(i, h) rep(j, w) searched[i][j] = false;
        
        int cnt = 0;
        rep(i, h) rep(j, w) {
            if (m[i][j] == 0) continue;
            if (searched[i][j]) continue;
            ++cnt;
            DFS(i, j);
        }
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[50][50]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>a[i][j];

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

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];

	if(nx>=0&&nx<50&&ny>=0&&ny<50&&a[ny][nx]==1)
	  dfs(nx,ny);
    }
  }

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] = {0};

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

	for(int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 and nx <= w-1 and ny >= 0 and ny <= h and field[ny][nx]==1){
			dfs(nx, ny);
		}
	}
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		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){
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int w,h;
int t[52][52];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};


void solve(int x,int y){
  t[y][x] = 0;
  rep(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(w>nx && nx>=0 && h>ny && ny>=0 && t[ny][nx]){
      solve(nx,ny);
    }
  }
}

int main(){
  while(cin>>w>>h,w||h){
    rep(i,h){
      rep(j,w){
        cin>>t[i][j];
      }
    }

    int cnt = 0;
    rep(i,h){
      rep(j,w){
        if(t[i][j]){
          solve(j,i);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int c[52][52];

void er(int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(i+1,j-1);
    else if(c[i][j+1])
        er(i,j+1);
    else if(c[i+1][j])
        er(i+1,j);
    else if(c[i+1][j+1])
        er(i+1,j+1);
    else if(c[i-1][j+1])
        er(i-1,j+1);
    else if(c[i-1][j])
        er(i-1,j);
    else if(c[i-1][j-1])
        er(i-1,j-1);
    else if(c[i][j-1])
        er(i,j-1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}