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<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
static const ll maxLL = (ll)1 << 62;

//dfs(bool型)
int dy[]={-1,0,1,0,1,-1,-1,1};
int dx[]={0,1,0,-1,1,1,-1,-1};
bool visited[1000][1000];
int t[1000][1000];
char copy_t[1000][1000];
int h,w;
int si,sj;

void dfs(int y,int x){
  if(y<=0||x<=0)return;
  if(y>h||x>w)return;
  if(t[y][x]==0)return;
  if(visited[y][x])return;
  visited[y][x]=true;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    dfs(ny,nx);
  }
  return;
}

int main(){
    while(0){
    int cnt=0;
    cin>>w>>h;
    if(w==\0 && h==\0)break;
    repe(i,1,h){
        for(int j=1;j<=w;j++){
            cin>>t[i][j];
        }
    }
    repe(i,1,h){
        repe(j,1,w){
             if(t[i][j]==1 && visited[i][j]==false)cnt++;
        dfs(i,j);
        }
    }
    cout<<cnt<<endl;
}
return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
#define P pair<int,int>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

int main(){
    
    while(true){
        int h,w;
        int m[50][50];
        bool used[50][50]={0};
        int dx[]={-1,0,1};
        int dy[]={-1,0,1};
        int ans=0;
        
        cin >> w>>h;
        if(h==0&&w==0)break;
        
        rep(i,h){
            rep(j,w){
                cin >> m[i][j];
            }
        }
        
        rep(i,h){
            rep(j,w){
                if(m[i][j]==1&&!used[i][j]){
                    ans++;
                    queue<P> que;
                    que.push(P(i,j));
                    while(!que.empty()){
                        P p=que.front();que.pop();
                        int x = p.second,y=p.first;
                        rep(a,3){
                            rep(b,3){
                                int nx=x+dx[a],ny=y+dy[b];
                                if(nx>=0&&nx<w&&ny>=0&&ny<h&&m[ny][nx]==1&&!used[ny][nx]){
                                    que.push(P(ny,nx));
                                    used[ny][nx]=true;
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << ans<<endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());


int ans = 0;
vector<vector<int> > island_map;
void dfs(vector<vector<int> > field, pair<int, int> init_pos)
{
  int dirx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int diry[] = {1, 0, -1, 1, -1, 1, 0, -1};

  int h = field.size();
  int w = field[0].size();
  stack<pair<int, int> > pos_stack;
  pos_stack.push(init_pos);

  while (pos_stack.size())
  {
    pair<int, int> pos = pos_stack.top();
    pos_stack.pop();
    int pos_x = pos.first;
    int pos_y = pos.second;
    rep(i, 8)
    {
      int next_x = pos_x + dirx[i];
      int next_y = pos_y + diry[i];

      if (0 <= next_x && next_x < h && 0 <= next_y && next_y < w && island_map[next_x][next_y] == 0 && field[next_x][next_y] == 1)
      {
        island_map[next_x][next_y] = ans;
        pos_stack.push(make_pair(next_x, next_y));
      }
    }
  }
}

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

    ans = 0;    
    vector<vector<int> > field(h, vector<int>(w, 0));
    island_map = vector<vector<int> >(h, vector<int>(w, 0));
    rep(i, h) rep(j, w)
    {
      cin >> field[i][j];
    }

    rep(i, h) rep(j, w)
    {
      if(island_map[i][j] == 0 && field[i][j] == 1)
      {
        ans++;
        dfs(field, make_pair(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}