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>
using namespace std;
int needx[]={-1,0,1,0,1,1,-1,-1};
int needy[]={0,1,0,-1,1,-1,1,-1};
//テ・ツウツカテ」ツつ津・ツ?ィテ」ツ?ヲfalseテ」ツ?ォテ・ツ、ツ嘉」ツ?暗」ツ?ヲtrueテ」ツつ津ィツソツ氾」ツ?凖ゥツ鳴「テヲツ閉ー//
bool land(int x,int y,vector<vector<bool> >& cango){
    if(cango[x][y]){
        cango[x][y]=false;
        for(int i=0;i<8;i++){
            int nextx=x+needx[i];
            int nexty=y+needy[i];
            if(cango[nextx][nexty])
                bool flag=land(nextx,nexty,cango);
            }
    return true;
     }else{
return false;
    }
}
int main(void){
    int input;
	int w,h;
	while(1){
cin>>w>>h;
if(h==0)break;
    vector<vector<bool> > cango(h+2,vector<bool>(w+2,false));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >>input;
            if(input==1){
cango[i][j]=true;
            }
        }
    }
  
    int t=0;
for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
        if(land(i,j,cango)){
            t++;
        }
    }
}
cout <<t<<endl;
	}
return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }

  }

}

int main(){
  int w,h;
  while(cin>>w>>h&&w>0&&h>0){

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


    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAX_LEN 50
int board[MAX_LEN][MAX_LEN];
bool used[MAX_LEN][MAX_LEN];
int w,h;

bool isin(int x, int y){
  return (x>=0 && x<w && y>=0 && y<h);
}

void dfs(int x, int y){
  if( !isin(x, y) )return;
  if(used[y][x])return;
  if(board[y][x]==0)return;
  used[y][x]=true;

  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(){
  while(true){
    cin>>w>>h;
    if(w==0 && h==0)break;
    rep(i, MAX_LEN)rep(j, MAX_LEN){
      board[i][j]=0;
      used[i][j]=false;
    }

    rep(i, h)rep(j, w)cin>>board[i][j];

    int result=0;
    rep(i, h)rep(j, w){
      if(board[i][j]==1 && !used[i][j])result++;
      dfs(j, i);
    }

    // rep(i, h){
    //   rep(j, w)cout<<board[i][j];
    //   cout<<endl;
    // }

    cout<<result<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll c[100][100];
bool b[100][100];
ll dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}, dy[8] = {1, -1, 0, 1, -1, 0, -1, 1};
ll w, h;

void dfs(ll x, ll y) {
    b[x][y] = true;
    rep(i, 8) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1) {
            if (b[nx][ny]) continue;
            dfs(nx, ny);
        }
    }
}

int main() {
    vector<ll> vec;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) {
            for(auto v : vec) cout << v << endl;
            return 0;
        }

        rep(i, h) rep(j, w) cin >> c[i][j];
        rep(i, h) rep(j, w) b[i][j] = false;

        ll ans = 0;
        rep(i, h) rep(j, w) {
            if (c[i][j] == 1 && b[i][j] == false) {
                ans++;
                dfs(i, j);
            }
        }
        vec.push_back(ans);
    }
}