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 namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = INT_MAX;
const ll LLINF = INT64_MAX;
const double PI = acos(-1);
const int MOD = 1000000007;

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

using P = pair<int, int>;

void dfs(int x, int y, int h, int w, vector<vector<int>> &g, vector<vector<bool>> &seen)
{
    stack<P> st;
    st.push(P(x, y));
    while (st.size())
    {
        P now = st.top();
        st.pop();
        seen[now.second][now.first] = true;
        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                int nx = now.first + dx;
                int ny = now.second + dy;
                if (0 <= nx && nx < w && 0 <= ny && ny < h && !seen[ny][nx] && g[ny][nx] == 1)
                {
                    st.push(P(nx, ny));
                }
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true)
    {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0)
        {
            return 0;
        }
        // 陸は1、海は0
        vector<vector<int>> g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                cin >> g[i][j];
            }
        }
        vector<vector<bool>> seen(h, vector<bool>(w, false));
        int ans = 0;
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (!seen[y][x] && g[y][x] == 1)
                {
                    dfs(x, y, h, w, g, seen);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

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

bool check(int x, int y){
  if(x < 0 || y < 0)return false;
  if(x >= w || y >= h)return false;
  if(visited[y][x])return false;
  if(c[y][x] == 0)return false;
  return true;
}

void dfs(int x, int y){ 
  if(!check(x, y))return;

  visited[y][x] = true;
  for(int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i]);
  }
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        scanf("%d", &c[i][j]);
      }
    }

    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        visited[i][j] = false;
      }
    }

    int cnt = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!visited[i][j] && c[i][j] == 1){
          dfs(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>

using namespace std;

#define WH 52

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

void dfs(int,int);

int main(int argc, char const *argv[])
{
	int w,h;


	int cnt;

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

		for(int i1=0;i1<WH;i1++){
			for(int i2=0;i2<WH;i2++){
				f[i1][i2]=0;
			}
		}

		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				cin>>f[i1][i2];
			}
		}

		cnt=0;
		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				if(f[i1][i2]==1){
					f[i1][i2]=0;
					dfs(i2,i1);
					cnt++;
				}
			}
		}

		cout<<cnt<<endl;

	}

	return 0;
}

void dfs(int x,int y){
	for(int i1=0;i1<8;i1++){
		for(int i2=0;i2<8;i2++){
			if(f[y+dy[i1]][x+dx[i2]]==1){
				f[y+dy[i1]][x+dx[i2]]=0;
				dfs(x+dx[i2],y+dy[i1]);
			}
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int draw ( int width, int height,std::vector<std::vector<int>> map){
	for(int i =0; i < height; i ++){
		for(int j = 0; j < width; j ++){
			cout << map[j][i] << " ";
		}
		cout << endl;

	}
	return 0;
}
//未探索陸地か
bool judge (int width,int height,std::vector<std::vector<int>> map, int x, int y){
	if( x >=0 && x  < width && y >= 0 && y < height && map[x][y] == 1){
		return true;
	}
	return false;
}
//周辺を探査して塗りつぶす
std::vector<std::vector<int>> Conqure (int width,int height,std::vector<std::vector<int>> map, int x, int y, int color) {
	map[x][y] = color;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			if(judge(width,height,map,x+dx,y+dy)){
				map[x+dx][y+dy] = color;
				if(!(dx == 0 && dy == 0)){
					map = Conqure(width,height,map,x+dx,y+dy,color);
				}
				
			}
		}
	}
	return map;
}
int main () {
	while(true){
			int w,h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	std::vector<std::vector<int>> map;
		map.resize(w);		// ()内の数字が要素数になる
	for( int i=0; i<w; i++ ){
		map[i].resize(h);
	}
	int landCount = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> map[j][i];
		}
	}
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(judge(w,h,map,j,i)){
				landCount += 1;
				map = Conqure(w,h,map,j,i,landCount + 1);
				//draw(w,h,map);
			}
		}
	}
	cout <<  landCount << endl;
	}

}