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 map[114][514];
int h, w;
bool dfs(int i, int j) {
	map[i][j] = 0;
	if (i > 0) {
		if (j > 0) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j < w) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j > 0) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j < w) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i < h) {
		if (j > 0) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j < w) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)cin >> map[i][j];
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)if (map[i][j] == 1) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}

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

void aruku(int** house,int w,int h,int row,int col){
	if(house[col][row]==0)return;
	house[col][row]=0;
	if(row+1<w)aruku(house,w,h,row+1,col);
	if(row+1<w && col+1<h)aruku(house,w,h,row+1,col+1);
	if(col+1<h)aruku(house,w,h,row,col+1);
	if(row-1>=0 && col+1<h)aruku(house,w,h,row-1,col+1);
	if(row-1>=0)aruku(house,w,h,row-1,col);
	if(row-1>=0 && col-1>=0)aruku(house,w,h,row-1,col-1);
	if(col-1>=0)aruku(house,w,h,row,col-1);
	if(row+1<w && col-1>=0)aruku(house,w,h,row+1,col-1);
return;
}

int tansaku( int** house,int w,int h){
	int row=0;int col=0;int cnt=0;
	for(int i=0;i<h;i++)
	for(int j=0;j<w;j++){
		if(house[i][j]==1){row=j;col=i;cnt++;
			aruku(house,w,h,row,col);
		}
	}
	return cnt;
}


int main(void){
int h,w;
	
	do{
	cin>>w;cin>>h;
	int** house=(int**)calloc(h,sizeof(int*));
	for(int i=0;i<h;i++)house[i]=(int*)calloc(w,sizeof(int));

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){cin>>house[i][j];}
	}
	
	int ret=tansaku(house,w,h);
	if(w!=0 && h!=0)cout<<ret<<endl;
	for(int i=0;i<h;i++)free(house[i]);
	free(house);
	}while(w!=0 && h!=0);
	
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#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 irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//???

int w,h,field[50][50],ans;

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

int main(void){
	while(1){
		ans=0;
		cin>>w>>h;
		if(w==0 && h==0)break;
		rep(i,h){
			rep(j,w){
				cin>>field[j][i];
			}
		}
		rep(i,h){
			rep(j,w){
				if(field[j][i]==1){
					dfs(j,i);
					ans++;
					// cout<<i<<","<<j<<endl;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll
int w,h;

void f(int c[100][100],bool b[100][100],int y,int x){

  if(y<0||x<0||y>h||x>w||!c[y][x]||b[y][x]) return;
  b[y][x]=true;
  f(c,b,y+1,x);
  f(c,b,y-1,x);
  f(c,b,y,x+1);
  f(c,b,y,x-1);
  f(c,b,y+1,x+1);
  f(c,b,y+1,x-1);
  f(c,b,y-1,x+1);
  f(c,b,y-1,x-1);
}



signed main(){
  while(1){
    
    cin>>w>>h;
    if(w==0) break;

    int c[100][100]={};
    rep(i,h){
      rep(j,w){
        cin>>c[i][j];
      }
    }

    bool b[100][100]={};

    int s=0;

    rep(i,h){
      rep(j,w){
        if(c[i][j]&&!b[i][j]){
          f(c,b,i,j);
          s++;
        }
      }
    }

    cout<<s<<endl;
  }
}