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 <list>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;
using vs=vector<string>;
using vvi=vector<vi>;
using vpii=vector<pii>;
using si=set<int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF (int)1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);
bool isInnerIndex(int kx,int ky,int mx,int my){ return (kx>=0&&kx<mx&&ky>=0&&ky<my); }

int x,y;
vvi box;
int dx[]{0,1,0,-1,1,1,-1,-1};
int dy[]{1,0,-1,0,-1,1,-1,1};
ll s=0;
void dfs(int px,int py){
    box[py][px]=2;
    REP(i,8){
        int nx=px+dx[i];
        int ny=py+dy[i];
        if(isInnerIndex(nx,ny,x,y)){
            if(box[ny][nx]==1)dfs(nx,ny);
        }
    }
    return;
}

int main(){
    vi ans;
    while (cin>>x>>y,(x||y))
    {
        box.clear();
        box.resize(y);
        REP(i,y) REP(n,x) {
            int c;
            cin>>c;
            box[i].pb(c);
        }
        ll s=0;
        REP(i,y)REP(n,x){
            if(box[i][n]==1) {dfs(n,i);s++;}
        }
        ans.pb(s);
    }
    
    ShowAll(ans);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>

int H,W;
int c[55][55];
vector<vector<int > > s(55,vector<int>(55));
bool seen[55][55];
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
void dfs(int y,int x,int a){
  if(x<0||W<=x||y<0||H<=y||c[y][x]==0){return;}
  if(seen[y][x]){return;}
  seen[y][x]=true;s[y][x]=a;
  for(int i=0;i<8;i++){
    dfs(y+dy[i],x+dx[i],a);
  }
}

int main() {
 while(cin>>W>>H){
  if(W==0&&H==0){return 0;}
 // vector<vector<int > > s(55,vector<int>(55));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      s[i][j]=0;
    }
  }
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>c[i][j];
    }
  }
  for(int i=0;i<55;i++){
    for(int j=0;j<55;j++){
      seen[i][j]=false;
    }
  }
  int a=1;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(c[i][j]){dfs(i,j,a);a++;}
    }
  }
  set<int>k;k.insert(0);
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(s[i][j]){k.insert(s[i][j]);}
    }
  }
  cout<<k.size()-1<<endl;
 }
  return 0;
}

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

using namespace std;
vector< vector<int> > map( 51 ,vector<int>(51,0));
int h,w;

void serch_island(int x,int y){
  map[y][x] = 0;
  const int dirX[] = {1 ,0, -1,  0, 1 , 1 , -1, -1};
  const int dirY[] = {0 ,1,  0, -1, 1 ,-1 ,  1, -1};
    for(int dir = 0; dir < 8; dir++){
      int nx = x + dirX[dir];
      int ny = y + dirY[dir];
      /*
      for(int i = 0; i < h ; i++){
        for(int j = 0; j < w; j++){
          cout << map[i][j] ;
        }
        cout << endl;
      }
      cout << endl;//*/
      if(nx >= 0 && nx < w &&
         ny >= 0 && ny < h &&
         map[ny][nx] == 1){
        serch_island(nx,ny);
      }
    }
    return ;
}

void solve(){
  int island = 0;

  for(int i = 0; i < h ; i++){
    for(int j = 0; j < w; j++){
      if(map[i][j] == 1){
        island++;
        serch_island(j, i);
      }
    }
  }
  cout << island << endl;
  map.clear();
  return ;
}
int main(){
  while(true){
    cin >> w >> h;
    if(h + w == 0) break;
    for(int i = 0; i < h ; i++){
      for(int j = 0; j < w; j++){
        cin >> map[i][j];
      }
    }
    solve();
  }
  return 0;
}