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>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

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

void dfs(int x, int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int nx = x + i, ny = y + j;
			if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&c[nx][ny])
				dfs(nx, ny);
		}
	}
}
int main() {
	while (scanf("%d%d", &w, &h), w) {
		rep(i, h)rep(j, w)scanf("%d", &c[i][j]);
		int cnt = 0;
		rep(i, h)rep(j, w) {
			if (c[i][j]) {
				dfs(i, j);
				cnt++;
			}
		}
		printf("%d\n", cnt);
	}
}

C++: #include <iostream>
using namespace std;
int W,H,c[50][50];
void island(int h, int w){
    c[h][w]=0;
    for(int i=h-1; i<h+2; i++){
        for(int j=w-1; j<w+2; j++){
            if(c[i][j]==1 && i>=0 && j>=0 && i<H && j<W) island(i,j);
        }
    }
}

int main(void){
    int count;
    while(1){
        cin>>W>>H;
        count=0;
        if(W==0 && H==0) break;
        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++){
                if(c[i][j]==1){
                    count++;
                    island(i,j);
                }
            }
        }
        cout<<count<<endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int island[52][52];
void func(int,int);

int main(){
  int w,h;
  int ans;
  
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    ans=0;
    fill(island[0],island[52],0);
    
    for(int i=1;i<=h;i++){
      for(int l=1;l<=w;l++){
        cin>>island[i][l];
      }
    }
    
    for(int i=1;i<=h;i++){
      for(int l=1;l<=w;l++){
        if(island[i][l]==1){
          func(i,l);
          ans++;
        }
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

void func(int x,int y){
  island[x][y]=0;
  for(int i=x-1;i<x+2;i++){
    for(int l=y-1;l<y+2;l++){
      if(island[i][l]==1)
        func(i,l);
    }
  }
}

C++: #include <stdio.h>
int m[51][51]={};
void dfs(int i,int j){
 m[i][j]=1;
 if(m[i+1][j]==2)dfs(i+1,j);
 if(m[i+1][j+1]==2)dfs(i+1,j+1);
 if(m[i][j+1]==2)dfs(i,j+1);
 if(m[i-1][j+1]==2)dfs(i-1,j+1);
 if(m[i-1][j]==2)dfs(i-1,j);
 if(m[i-1][j-1]==2)dfs(i-1,j-1);
 if(m[i][j-1]==2)dfs(i,j-1);
 if(m[i+1][j-1]==2)dfs(i+1,j-1);
}

int main(){
  int cou,i,j,y,t,a;
 while(1){
   cou=0;
   scanf("%d %d",&y,&t);
   if(y==0&&t==0)break;
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	scanf("%d",&a);
	a++;
	m[i][j]=a;
     }
   }
   for(i=1;i<=t;i++){
     for(j=1;j<=y;j++){
	if(m[i][j]==2){
	  cou++;
	  dfs(i,j);
	}
     }
   }
   printf("%d\n",cou);
 }
 return 0;
}

C++: //INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
//DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a)  (a).begin(),(a).end()
#define ALL(a,n)  (a),(a)+n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
#define dcml(n) fixed<<setprecision(n)
//CONST
//------------------------------------------
const ll INF=1010000000000000017LL;
const ll MOD=1000000007LL;
const ld EPS=1e-12;
const ld PI=3.14159265358979323846;
//REPEAT
//------------------------------------------
#define   FOR(i,m,n) for(ll (i)=(m);   (i)<(n);  (i)++)
#define  FORS(i,m,n) for(ll (i)=(m);   (i)<=(n); (i)++)
#define  RFOR(i,m,n) for(ll (i)=(n)-1; (i)>=(n); (i)--)
#define RFORS(i,m,n) for(ll (i)=(n);   (i)>(n);  (i)--)
#define   REP(i,n)   for(ll (i)=0;     (i)<(n);  (i)++)
#define  REPS(i,x)   for(ll (i)=1;     (i)<=(x); (i)++)
#define  RREP(i,x)   for(ll (i)=(x)-1; (i)>=0;   (i)--)
#define RREPS(i,x)   for(ll (i)=(x);   (i)> 0;   (i)--)
#define  WREP(i,in,j,jn) REP(i,in)REP(j,jn)
#define WREPS(i,in,j,jn) REPS(i,in)REPS(j,jn)
//-----------------------------------------
//namespace
using namespace std;
//ライブラリはここに
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
vi dx8={-1,-1,-1,0,0,1,1,1};
vi dy8={-1,0,1,-1,1,-1,0,1};
//-----------------------------------------
ll w,h;
int c[50][50];
bool is;
void dfs(int x,int y){
    if(c[x][y]==0)return;
    c[x][y]=0;
    //cout<<x<<y<<"->";
    is=true;
    REP(i,8){
        int X=x+dx8[i],Y=y+dy8[i];
        if(0<=X&&X<h and 0<=Y&&Y<w){
            if(c[X][Y]==0)continue;
            dfs(X,Y);
        }
    }
}
int main(void){
    while(cin>>w>>h and !(w==0&&h==0) ){
        //cout<<"w:"<<w<<"h:"<<h<<"\n";
        int cnt=0;
        REP(i,h)REP(j,w)cin>>c[i][j];
        REP(i,h)REP(j,w){
            is=false;
            dfs(i,j);//cout<<"\n";
            if(is)cnt++;
        }
        cout<<cnt<<"\n";
    }
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w == 0 || h == 0){
    }
    else{
        cout << ans << endl;
    }
    return 0;
}