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>
#include<string.h>

int main(){
	int zu[52][52],w,h,i,j,flag,land;
	
	while(0<=scanf("%d%d",&h,&w)){
		if(w==0&&h==0)break;
		
		memset(zu,0,sizeof(zu));
		
		for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
				scanf("%d",&zu[i][j]);
			}
		}
		
		flag=1;
		land=1;
		while(flag!=0){
			++land;
			flag=0;
			for(i=1;i<=w;i++){
				for(j=1;j<=h;j++){
					if(zu[i][j]==1){
						zu[i][j]=land;
						flag=1;
						break;
					}
				}
				if(flag==1)break;
			}
			
			while(flag==1){
				flag=2;
				for(i=1;i<=w;i++){
					for(j=1;j<=h;j++){
						if(zu[i][j]!=1)continue;
						if(zu[i+1][j]==land || zu[i-1][j]==land || zu[i][j+1]==land || zu[i][j-1]==land ||
							zu[i+1][j+1]==land|| zu[i+1][j-1]==land||zu[i-1][j+1]==land||zu[i-1][j-1]==land){
							zu[i][j]=land;
							flag=1;
						}
					}
				}
			}
		}
		for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
//				printf("%d ",zu[i][j]);
			}
//			printf("\n");
		}
		
		
		printf("%d\n",land-2);
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int w, h, cnt = 0;

void srch(int x, int y, vector<vector<int>> &inp, vector < vector <bool>> &hit)
{
	if (x < 0 || w <= x || y < 0 || h <= y || inp[y][x] == 0) { return; }
	if (hit[y][x]) return;
	hit[y][x] = true;
	cnt += 1;
	srch(x + 1, y, inp, hit);
	srch(x - 1, y, inp, hit);
	srch(x, y + 1, inp, hit);
	srch(x, y - 1, inp, hit);
	srch(x + 1, y + 1, inp, hit);
	srch(x + 1, y - 1, inp, hit);
	srch(x - 1, y + 1, inp, hit);
	srch(x - 1, y - 1, inp, hit);
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> w >> h && w != 0 && h != 0)
	{
		vector<vector<int>> inp(h, vector<int>(w, 0));
		vector<vector<bool>> hit(h, vector<bool>(w, false));
		int ans = 0;
		rep(y, h) rep(x, w){ cin >> inp[y][x]; }
		rep(y, h) rep(x, w)
		{
			srch(x, y, inp, hit);
			if (cnt > 0) { ans += 1; }
			cnt = 0;
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;

template<typename A,typename B> inline void chmin(A &a,B b){if(a>b) a=b;}
template<typename A,typename B> inline void chmax(A &a,B b){if(a<b) a=b;}

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

//-----------------------------------------------------------------------

int w,h;
int M[55][55];

void dfs(int y,int x)
{
    M[y][x]=0;
    REP(i,8){
        int ny=y+dy[i],nx=x+dx[i];
        if(0<=ny && ny<h && 0<=nx && nx<w && M[ny][nx]) dfs(ny,nx);
    }
}   

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

    while(cin>>w>>h,w|h)
    {
        REP(i,h) REP(j,w) cin>>M[i][j];

        int cnt=0;
        REP(i,h) REP(j,w){
            if(M[i][j]){
                cnt++;
                dfs(i,j);
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

char maze[50][50];
int H,W;

int dfs(int x,int y){
  if(x<0 || H<=x || y<0 || W<=y)return 0;
  int cnt = 0;
  maze[x][y] = '0';
  int dx[] = { 1, 1, 1,-1,-1,-1, 0, 0};
  int dy[] = {-1, 0, 1,-1, 0, 1, 1,-1};
  rep(i,8){
    // if(i==j&&i==0)continue;
    if(maze[x+dx[i]][y+dy[i]]=='1')dfs(x+dx[i],y+dy[i]);
  }
  return cnt;
}
void solve(){
  int h,w;
  while(cin>>W>>H,H+W){
    rep(i,H)rep(j,W)cin>>maze[i][j];
    int res=0;
    rep(i,H)rep(j,W){
      //rep(k,H){rep(l,W)cout<<maze[k][l];cout<<endl;}
      //cout<<endl;
      if(maze[i][j]=='1'){
	dfs(i,j);
	res++;
      }
    }
    cout<<res<<endl;
  }
}

int main(void){
  solve();
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int H, W;
int c[50][50];

int dfs(int x, int y){
    c[x][y] = 0;
    rep2(i, -1, 1){
        rep2(j, -1, 1){
            int nx = x+i, ny = y+j;
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(c[nx][ny] == 0) continue;
            dfs(nx, ny);
        }
    }
    return 1;
}

int main(){
    while(true){
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        rep(i, H){
            rep(j, W) cin >> c[i][j];
        }
        int ans = 0;
        rep(i, H){
            rep(j, W){
                if(c[i][j]) ans += dfs(i, j);
            }
        }
        cout << ans << endl;
    }
}