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 <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int W, H, C[100][100];
const int di[3] = {-1, 0, 1}, dj[3] = {-1, 0, 1};
bool visited[100][100];

int Solve(){
  fill((bool*)visited, (bool*)(visited + H), false);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> C[i][j];
    }
  }
  
  int nowi = 0, ans = 0;
  queue <Pii> que;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (C[i][j] == 1){
        que.push(make_pair(i, j));
        nowi = i; visited[i][j] = true;
        ans++;
        break;
      }
    }
    if (!que.empty()) break;
  }
  
  while (!que.empty()){
    Pii Q = que.front(); que.pop();
    int I = Q.first, J = Q.second;

    for (int i = 0; i < 3; i++){
      for (int j = 0; j < 3; j++){
        if (0 <= I + di[i] && I + di[i] < H && 0 <= J + dj[j] && J + dj[j] < W){
          if (C[I + di[i]][J + dj[j]] == 1 && visited[I + di[i]][J + dj[j]] == false){
            que.push(make_pair(I + di[i], J + dj[j]));
            visited[I + di[i]][J + dj[j]] = true;
          }
        }
      }
    }

    if (que.empty()){
      for (int i = nowi; i < H; i++){
        for (int j = 0; j < W; j++){
          if (C[i][j] == 1 && visited[i][j] == false){
            que.push(make_pair(i, j)); ans++; nowi = i;
            visited[i][j] = true; break;
          }
        }
        if (!que.empty()) break;
      }
    }
  }
  cout << ans << endl;
  
  return 0;
}

int main(){
  while (cin >> W >> H){
    if (W == 0 && H == 0) return 0;
    Solve();
  }
  
  return 0;
}

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

using namespace std;

void fill(vector< vector<int> >& table, int i, int j, int w, int h, int e){
    if(i < 0 || j < 0) return;
    if(i >= h || j >= w) return;
    for(int k=-1; k <= 1; ++k){
        for(int l=-1; l <= 1; ++l){
            if(i+k < 0 || j+l < 0) continue;
            if(i+k >= h || j+l >= w) continue;
            if(table[i+k][j+l] == 1) {
                table[i+k][j+l] = e;
                fill(table, i+k, j+l, w, h, e);
            }
        }
    }
    return;
}

int main(){
    while(true){
        int w, h;
        int cnt = 1;
        cin >> w;
        cin >> h;
        if(w == 0 && h == 0) break;
        vector< vector<int> > table(h, vector <int>(w));
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> table[i][j];
            }
        }
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                int e = table[i][j];
                if(e == 0) continue;
                else if(e == 1) {
                    cnt++; e = cnt; table[i][j] = e;
                    fill(table, i, j, w, h, e);
                }
            }
        }
        /*
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/
        cout << (cnt-1) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){

			if(field[x+xx][y+yy]==1){
				land(x+xx,y+yy);
			}

		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

	scanf("%d %d",&w,&h);

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[i][j]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);

}

C++: #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10000000
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
typedef vector<int> v;
typedef vector<int, int> v2;
typedef vector< pair<int, int> > vp;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int w, h;
int c[52][52];
int f[52][52];

bool dfs(int y, int x){
	if (c[y][x] == 0) return false;
	if (f[y][x] == 1) return false;
	
	f[y][x] = 1;
	if (y > 0) dfs(y - 1, x);
	if (x > 0) dfs(y, x - 1);
	
	if (y < h) dfs(y + 1, x);
	if (x < w) dfs(y, x + 1);

	if (y > 0 && x > 0) dfs(y - 1, x - 1);
	if (y < h && x > 0) dfs(y + 1, x - 1);
	if (y > 0 && x < w) dfs(y - 1, x + 1);
	if (y < h && x < w) dfs(y + 1, x + 1);

	return true;
}

int main(){
	ios::sync_with_stdio(false);

	while (1){
		cin >> w >>  h;
		if (w == 0 && h == 0) break;

		int ans = 0;

		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> c[i][j];
			
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				f[i][j] = -1;
		
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if(dfs(i, j)) ans++;

		cout << ans << endl;

	}

	return 0;
}