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>

using visited = std::vector<std::vector<bool>>;
using marine_map = std::vector<std::vector<int>>;

int dx[] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy[] = {1, 1, 1, 0, 0, -1, -1, -1};
int w, h, c;

void dfs(int x, int y, visited& v, marine_map& m, int& c) {
    if (v[y][x]) {
        return;
    }
    v[y][x] = true;
    for (int p = 0; p < 8; p++) {
        int cy = y + dy[p];
        int cx = x + dx[p];
        if (cx < 0 || cx >= w || cy < 0 || cy >= h || m[cy][cx] != 1) {
            continue;
        }
        dfs(cx, cy, v, m, c);
    }
}


int main() {
    while (true) {
        std::cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        c = 0; // reset counter for new map;
        std::vector<std::vector<int>> vec(h, std::vector<int>(w, 0));
        std::vector<std::vector<bool>> bools(h, std::vector<bool>(w, false));
        for (int i = 0; i < h; i++) {
            for (int p = 0; p < w; p++) {
                std::cin >> vec[i][p];
            }
        }
        for (int i = 0; i < h; i++) {
            for (int p = 0; p < w; p++) {
                if (vec[i][p] == 1 && !bools[i][p]) {
                    c++;
                    dfs(p, i, bools, vec, c);
                }
            }
        }
        std::cout << c << std::endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

int c[100][100];//0??????
int h,w;

int dx[8]={1,1,0,-1,-1,-1,0,1},dy[8]={0,1,1,1,0,-1,-1,-1};

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    c[i][j]=0;
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
                c[nx][ny]=0;
            }
        }
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
      cin>>w>>h;
      if(w==0&&h==0) break;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          cin>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int g[52][52];
void solve(int x,int y){
  g[x][y]=0;
  for(int i=-1;i<=1;++i){
    for(int j=-1;j<=1;++j){
      if(g[x+i][y+j]) solve(x+i,y+j);
    }
  }
}
int main(){
  int w,h,cnt;
  while(cin>>w>>h&&w&&h){
    cnt=0;
    for(int i=0;i<=h+1;++i){
      for(int j=0;j<=w+1;++j){
        if(i==0||j==0||i==h+1||j==w+1) g[i][j]=0;
        else cin>>g[i][j];
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
        if(g[i][j]){
          solve(i,j);
          ++cnt;
        }
      }
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int w, h;
int c[51][51];

bool valid(int i, int j) {
    return (0 <= i && i < h && 0 <= j && j < w);
}

void go(int y, int x) {
    if (c[y][x] == 0) return ;
    c[y][x] = 0;

    for (int i=y-1; i<=y+1; ++i) {
        for (int j=x-1; j<=x+1; ++j) {
            if (valid(i, j)) go(i, j);
        }
    }
}

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

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

        for (int i=0; i<h; ++i) {
            for (int j=0; j<w; ++j) {
                cin >> c[i][j];
            }
        }

        int ans = 0;
        for (int i=0; i<h; ++i) {
            for (int j=0; j<w; ++j) {
                if (c[i][j]) {
                    go(i, j);
                    ans ++;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e7+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
int c[100][100];
int ans;
void dfs(int y, int x){
	c[y][x] = 0;
	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int ny = y + i, nx = x + j;//??????8???????????§?¨?
			if (ny >= 0 && ny < h&&nx >= 0 && nx < w){//?????????????????????
				//???????????£???????????¶???????????????????????°
				if (c[ny][nx] == 1)dfs(ny, nx);
			}
		}
	}
}

int main(){
	while (cin >> w >> h&&w + h){
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) cin >> c[i][j];
		ans = 0;
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (c[i][j] == 1){//??¶??????????????????
					solve(i, j);//???????????£???????????¶????????¶???
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}
}