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 <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] && next_x < w && next_x >= 0 && next_y < h && next_y >= 0){
	  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 <bits/stdc++.h>
using namespace std;

const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};
bool seen[50][50];
void dfs(vector<vector<int>> C, int h, int w, int a, int b){
    seen[a][b]=true;
    for (int i=0; i<8; i++) {
        int aa=a+dx[i];
        int bb=b+dy[i];
        if (aa<0 || bb<0 || aa>=h || bb>=w) continue;
        if (C[aa][bb]==0) {
            continue;
        }
        if (seen[aa][bb]==true) continue;
        dfs(C,h,w,aa,bb);
    }
}

int main() {
    int w,h;
   cin >> w >> h;
   int total=0;
   vector<int> x;
   while(w!=0 || h!=0) {
       total++;
   vector<vector<int>> C(h,vector<int>(w));
   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           int p;
           cin >> p;
           C[i][j]=p;
       }
   }
   int count=0;

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           seen[i][j]=false;
       }
   }

   for (int i=0; i<h; i++) {
       for (int j=0; j<w; j++) {
           if (seen[i][j]==true) continue;
           if (C[i][j]==0) continue;
           count++;
           dfs(C,h,w,i,j);
           
       }
   }


   x.push_back(count);
   int a,b;
   cin >> a >> b;
   w=a,h=b;
   }
   for (int i=0; i<total; i++) {
       cout << x[i] << endl;
   }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<queue>
#include<sstream>
#include<iomanip>

#define reps(i,j,k) for(int i=(j);i<(k);i++)
#define rep(i,j) reps(i,0,j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class S,class T>
ostream &operator<<(ostream &out, const pair<S,T> p){
	return out << "(" << p.fs << ", " << p.sc << ")";
}

template<class T>
ostream &operator<<(ostream &out, const vector<T> &v){
	out << "{";
	rep(i,v.size()) out << v[i] << ", ";
	return out << "}" << endl;
}
int c[50][50];
int main(){
	int w,h;
	int dx[] = {-1,  0,  1, -1, 1, -1, 0, 1};
	int dy[] = {-1, -1, -1,  0, 0,  1, 1, 1};
	while(cin >> w >> h, w){
		rep(i,h) rep(j,w) {
			cin >> c[i][j];
		}
		int cnt = 2;
		rep(i,h) rep(j,w){
			if(c[i][j] != 1) continue;
			c[i][j] = cnt;
			queue<pii> q;
			q.push(pii(i, j));
			while(!q.empty()){
				int x = q.front().sc, y = q.front().fs;
				q.pop();
				rep(k,8){
					int cx = x + dx[k], cy = y+dy[k];
					if(cx >= 0 && cy >= 0 && cy < h && cx < w && c[cy][cx] == 1){
						q.push(pii(cy, cx));
						c[cy][cx] = cnt;
					}
				}
			}
			cnt ++;
		}
		cout << cnt - 2 << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    ofstream outputfile("output.txt");
    int w, h;
    while(cin >> w >> h, w + h) {
        if(w == 0 && h == 0) break;
        int temp;
        rep(i, h) {
            rep(j, w) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

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

        outputfile << ans << endl;
    }

    return 0;
}