Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

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;
}