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<stdio.h>
int w,h;
int c[100][100];

int qx[1000000];
int qy[1000000];
int X,Y,left,right;

void push(int x,int y){
  right++;
  qx[right]=x;
  qy[right]=y;
}

void pop(){
  X=qx[left];
  Y=qy[left];
  left++;
}

void bfs(int sx,int sy){
  left=0;
  right=-1;
  push(sx,sy);
  c[sx][sy]=0;
  while(left<=right){
    pop();
    for(int nx=X-1;nx<=X+1;nx++){
      for(int ny=Y-1;ny<=Y+1;ny++){
        if(nx>=0 && ny>=0 && c[nx][ny]!=0){
          c[nx][ny]=0;
          push(nx,ny);
        }
      }
    }
  }
}

int main(){

  while(1){
    scanf("%d %d",&w,&h);
    if(w==0&&h==0)break;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }

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

    printf("%d\n",ans);
    
  }
  return 0;
}

C++: #include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<utility>//pair
#include<list>
#include<vector>//可変長配列
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int,int>
#define PP pair<P,int>
#define F first
#define S second
int dx[8] = { 1,0,-1,0,1,-1,-1,1 };
int dy[8] = { 0,1,0,-1,1,1,-1,-1 };


int w, h;
int t[100][100];
bool visit[100][100];

void dfs(int ny, int nx) {
    if (visit[ny][nx])
        return;
    visit[ny][nx] = 1;

    rep(i, 8) {
        int x = nx + dx[i], y = ny + dy[i];
        if (x < 0 || y < 0 || x >= w || y >= h)
            continue;

        if(t[y][x]==1)
            dfs(y, x);
    }
}
int main() {
    while (cin >> w >> h, w || h) {
        rep(i, h)rep(j, w)cin >> t[i][j];
        rep(i, h)rep(j, w)visit[i][j] = 0;
        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (!visit[i][j] && t[i][j] == 1) {
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n;
vector<vector<int> > C;
vector<vector<int> > D;

void bfs(int initCX, int initCY){
    queue<int> Q;
    Q.push(initCX);
    Q.push(initCY);
    D[initCX][initCY] = 0; // ?????????
    while(!Q.empty()){
        // ??????????´???????????????????
        int cx, cy;
        cx = Q.front();
        Q.pop();
        cy = Q.front();
        Q.pop();
        for(int i = -1; i <= 1; ++i){
            for(int j = -1; j <= 1; ++j){ // ??????????????????????????????
                if(C[cx + i][cy + j] == 1 && D[cx + i][cy + j] == -1){ // ???????????£??????????¨??????????
                    D[cx + i][cy + j] = D[cx][cy] + 1;
                    // ????????????????¨??????????????????????
                    Q.push(cx + i);
                    Q.push(cy + j);
                }
            }
        }
    }
}

int main(){
    int w, h;
    // index?????????????°???????????????£????????§???????????§h??¨w?????\?????????????????????????????????????????????????????????
    while(cin >> h >> w, w != 0 || h != 0){
        C = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                cin >> C[i][j];
            }
        }
        D = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                D[i][j] = -1;
            }
        }
        int n = 0;
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                if(C[i][j] == 1 && D[i][j] == -1){ // ???????????£??????????¨??????????
                    bfs(i, j);
                    ++n;
                }
            }
        }
        cout << n << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int w, int h){
	field[h][w] = 0;

	for(int nh=h-1;nh<h+2;nh++){
	    for(int nw=w-1;nw<w+2;nw++){
	      if(field[nh][nw]==1&&0<=nh&&0<=nw&&nh<h&&nw<w)dfs(nw,nh);
	    }
	  }
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			break;
		}

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

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					count++;
					dfs(j,i);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
using namespace std;

int n,m,a[50][50],b[50][50],ans;
bool inRange(int z,int l,int r){
	return z>=l&&z<r;
}
void fill(int x,int y){
	if(!a[x][y]||b[x][y])return;
	b[x][y]=true;
	for(int xx=-1;xx<=1;xx++)if(inRange(x+xx,0,n)){
		for(int yy=-1;yy<=1;yy++)if(inRange(y+yy,0,m)){
			fill(x+xx,y+yy);
		}
	}
}
int main(){
	while(scanf("%d%d",&m,&n),n*m){
		ans=0;
		memset(b,0,sizeof(int)*2500);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				scanf("%d",&a[i][j]);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(!b[i][j]&&a[i][j])
					ans++,fill(i,j);
		printf("%d\n",ans);
	}

	return 0;
}