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<iostream>

using namespace std;

bool map[50][50];
int h,w;
int dx[]={-1,0,1},dy[]={-1,0,1};
void solve(void);
void loop(int ,int );
void draw(void);
int main(){
	while(cin>>h>>w,h){
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cin>>map[i][j];
			}
		}
		solve();
	}
	return 0;
}
void solve(void){
	int cnt=0;
	for(int i=0;i<w;i++){
		for(int j=0;j<h;j++){
			if(map[i][j]){
				loop(i,j);	
				cnt++;	
			}
		}
	}
	cout<<cnt<<endl;
}
void loop(int x,int y){
	map[x][y]=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			
			if(map[x+dx[i]][y+dy[j]]){
				loop(x+dx[i],y+dy[j]);
			}
		}
	}
	return ;
}

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

void dfs(vector<vector<int> > &map, int x, int y){
    map[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nx = x + dx, ny = y + dy;
            if(1 <= nx && nx <= map.size()-2 && 1 <= ny && ny <= map[dx].size()-2 && map[nx][ny] == 1) dfs(map, nx, ny);
        }
    }
}

int main(){
    while(1){
        int W, H;
        cin >> W >> H;
        if(W == 0) break;
    
        vector<vector<int> > map(H+2, vector<int>(W+2));
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                cin >> map[i][j];
            }
        }
    
        int res = 0;
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(map[i][j] == 1){
                    dfs(map, i, j);
                    res++;
                }
            }
        }
    
        cout << res << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int w,h;
vvll a(50,vll(50));

void dfs(int x,int y){
     a.at(x).at(y)=0;
     rep2(dx,-1,2){
       rep2(dy,-1,2){
         int  nx=x+dx;
         int  ny=y+dy;
           if(0<=nx&&nx<h&&0<=ny&&ny<w){
                if(a.at(nx).at(ny)==1){
                    dfs(nx,ny) ;         
                           }
                       }
                   }
               }                                  
}


int main() {
   while(1){
   cin>>w>>h;
   if(w==0&&h==0)break;
   else{
   rep(i,h){
       rep(j,w)cin>>a.at(i).at(j);
   }
  
   int ans=0;
   rep(i,h){
       rep(j,w){
           if(a.at(i).at(j)==1){
               
               
               dfs(i,j);
               ans++;
               
           }
       }
   }
   
  
   cout<<ans<<endl;
   }
   }
    
}

C++: #include<iostream>
#include<stack>
using namespace std;
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};
int main()
{
	int n,m;
	while(cin>>n>>m,n) 
	{
		int f[m][n];
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				cin>>f[i][j];
			}
		}

		int ans=0;
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(f[i][j]==1)
				{
					ans++;
					stack<int> xs,ys;
					xs.push(j); ys.push(i);
					while(!xs.empty()) 
					{
						int x=xs.top(),y=ys.top();
						xs.pop(); ys.pop();
						f[y][x]=0;
						for(int k = 0; k < 8; ++k)
						{
							int nx=x+dx[k],ny=y+dy[k];
							if(0<=nx&&nx<n&&0<=y&&y<m&&f[ny][nx]==1)
							{
								xs.push(nx); ys.push(ny);
							}
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
  return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

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

void solve(int x, int y)
{
    if (x < 0 || h <= x || y < 0 || w <= y)
        return;
    if (c[x][y] == 0)
        return;
    c[x][y] = 0;
    for (int i = -1; i < 2; i++)
    {
        for (int j = -1; j < 2; j++)
        {
            solve(x + i, y + j);
        }
    }
}

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

    while (1)
    {
        cin >> w >> h;
        if (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] == 1)
                {
                    solve(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}