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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

void solve(int h, int w) {
  vector<vi> g(h + 2, vi(w + 2, -1));
  rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
    int c;
    cin >> c;
    if(c == 1) g[i][j] = 0;
  }
  int ans = 0;
  rep2(a, 1, h + 1) rep2(b, 1, w + 1) {
    if(g[a][b] != 0) continue;
    ans++;
    g[a][b] = ans;
    stack<pair<int, int>> st;
    st.push(P(a, b));
    while(!st.empty()) {
      int i = st.top().first;
      int j = st.top().second;
      st.pop();
      rep(k, 8) {
        int ni = i + dir[k];
        int nj = j + dir[k + 1];
        if(g[ni][nj] != 0) continue;
        g[ni][nj] = g[i][j];
        st.push(P(ni, nj));
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while(true) {
    int h, w;
    cin >> w >> h;
    if(h == 0 && w == 0) break;
    solve(h, w);
  }
}

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

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; 

void bfs(int sx, int sy)
{
  queue<pxy> q;
  q.push(pxy(sx, sy)); 
  while(!q.empty()){
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if(map[x][y]){
      map[x][y] = 0;
      for(int i = 0; i < 8; i++){
	int next_x = x + dx[i], next_y = y + dy[i];
	if(map[next_x][next_y]){
	  q.push(pxy(next_x, next_y));
	}
      }
    }
  }
}

int main()
{
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }else{
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  cin >> map[j][i];
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  if(map[j][i]){
	    ans++;
	    bfs(j, i);
	  }
      cout << ans << endl;
      ans = 0;
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define SIZE 51

bool f[SIZE][SIZE]; //true=land (1)
int w,h;

void wfs(int i, int j){
  //width-first search
  int d[]={1,0,-1};
  rep(di,3){
    int ni=i+d[di];
    if(ni<0 || ni>=h) continue;
    rep(dj,3){
      int nj=j+d[dj];
      if(nj<0 || nj>=w) continue;
      if(f[ni][nj]){
	f[ni][nj]=false;
	wfs(ni,nj);
      }
    }
  }
}

int main(){
  while(true){
    cin >>w>>h;
    if(w==0&&h==0) break;
    
    rep(i,h){
      rep(j,w){
	int d;
	scanf("%d", &d);
	if(d==0) f[i][j]=false;
	else  f[i][j]=true; //1=land
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
	if(f[i][j]){
	  ans++;
	  wfs(i,j);
	}
      }
    }

    cout << ans << endl;

  }
}

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

using namespace std;

int w,h;
int map[100][100];

void delIsland(int y,int x){

  queue<pair<int,int> > que;
  que.push(make_pair(y,x));


  int next[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
  while(!que.empty()){
    pair<int,int> now  = que.front();
    que.pop();

    if(now.first<0 || now.first>=h || now.second<0 || now.second>=w || map[now.first][now.second]!=1){
      continue;
    }else{
      map[now.first][now.second] = 0;
    }


    for(int i=0;i<8;i++){
      int ny = now.first + next[i][0];
      int nx = now.second + next[i][1];
      que.push(make_pair(ny,nx));
    }
  }
}

int main(){
  cin>>w>>h;
  while(w!=0 && h!=0){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
       cin>>map[i][j];
     }
   }

   int ans = 0;
   for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
     if(map[i][j] == 1){
       ans++;
       delIsland(i,j);
     }
   }
 }

 cout<<ans<<endl;
 cin>>w>>h;
}

return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int> > mp;
int w, h;


void hoge(int wo, int ho) {
	mp[wo][ho] = 0;
	REP(i, 8) {
		int nw = wo + qx[i];
		int nh = ho + qy[i];
		if(nw >= 0 && nw < w && nh >= 0 && nh < h && mp[nw][nh] == 1) hoge(nw, nh);
	}
}
int main(void) {
	while(true) {
	ll count = 0;
	cin>>w>>h;
		if(w == 0 && h == 0) return 0;
		swap(w, h);
	mp.resize(w);
	REP(i, w) mp[i].resize(h);
	REP(i, w) {
		REP(j, h) {
			cin>> mp[i][j];
		}
	}
	REP(i, w) {
		REP(j, h) {
			if(mp[i][j] == 1) {
				count++;
				hoge(i, j);
			}
		}
	}
	cout<<count<<endl;
}
}