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>
#include <cstdio>

using namespace std;

int W, H;

int field[52][52];
bool visited[52][52];
int tx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int ty[] = {1, 0, -1, -1, -1, 0, 1, 1};

void island(int y, int x)
{
  visited[y][x] = true;
  for (int i = 0; i < 8; i++) {
    if (!visited[y + ty[i]][x + tx[i]] && field[y + ty[i]][x + tx[i]] == 1) {
      island(y + ty[i], x + tx[i]);
    }
  }
}

void solve()
{
  fill_n(&visited[0][0], 52 * 52, false);
  int c = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (!visited[i][j] && field[i][j] == 1) {
	island(i, j);
	c++;
      }
    }
  }
  printf("%d\n", c);
}

int main()
{
  while (scanf("%d %d ", &W, &H) == 2) {
    if (W == 0 && H == 0) {
      return 0;
    }
    fill_n(&field[0][0], 52 * 52, 0);
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
	scanf("%d ", &field[i][j]);
      }
    }
    solve();
  }
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
 
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
 
using namespace std;
 
 int dx[8] = {1,0,-1,0,1,1,-1,-1};
 int dy[8] = {0,1,0,-1,-1,1,-1,1};

void dfs(vector<vector<int> > &map,int x,int y,int h, int w){
  
    if(x < 0 || x >=w || y < 0 || y >=h){
      return ;
    }

    if(map[y][x]==0){
      return;
    }

    map[y][x] = 0;

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

    return;
}


int main()
{

  while(true){

    int w,h;
    cin >> w >> h;

    if(w==0&&h==0){
      break;
    }

    vector<vector<int> > map(h,vector<int>(w,0));    
    int count=0;

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


   for(int i=0;i<w;i++){
    for(int k=0;k<h;k++){
      if(map[k][i]==1){
        dfs(map,i,k,h,w);
        count++;
      }
    }
   }
 


    cout << count << endl;

  }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
int w,h,fld[64][64];

void dfs(int y,int x){
    if(x<0||x>=w||y<0||y>=h)return;
    if(!fld[y][x])return;
    fld[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i||j)dfs(y+i,x+j);
        }
    }
}
int main(){
    while(scanf("%d%d",&w,&h),w||h){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&fld[i][j]);
            }
        }

        int cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(fld[i][j]){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][52],int b[][52],int x,int y){
  if(a[x][y]==0||b[x][y]==1)
    return 0;
  else{
    b[x][y]=1;
    f(a,b,x-1,y-1);
    f(a,b,x,y-1);
    f(a,b,x+1,y-1);
    f(a,b,x-1,y);
    f(a,b,x+1,y);
    f(a,b,x-1,y+1);
    f(a,b,x,y+1);
    f(a,b,x+1,y+1);
    return 1;
  }
}
int main(){
  int i,j;
  int a[52][52],b[52][52],w,h;
  int ct;
  while(cin>>w>>h&&w+h){
    memset(a,0,sizeof(a));
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	cin>>a[j][i];
    memset(b,0,sizeof(b));
    ct=0;
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	ct+=f(a,b,j,i);
    cout<<ct<<endl;
  }
  return 0;
}

C++: #include <functional>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            return 0;
        }
        vector<vector<int>> v(h, vector<int>(w, -1));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> v[i][j];
            }
        }
        constexpr int MIN_ID = 10;
        function<int(int, int, int)> f = [&v, &f, w, h, MIN_ID](int i, int j,
                                                                int id) {
            // ?????¢????¶???????
            if (i == -1 || j == -1 || i == h || j == w) {
                return 0;
            }
            // ?????§?????¶????±?????????????
            else if (MIN_ID <= v[i][j] && v[i][j] <= id) {
                return 0;
            }
            // ???
            else if (v[i][j] == 0) {
                return 0;
            }
            else {
                v[i][j] = id;
                int r = 1;
                r += f(i - 1, j - 1, id);
                r += f(i - 1, j, id);
                r += f(i - 1, j + 1, id);
                r += f(i, j - 1, id);
                r += f(i, j + 1, id);
                r += f(i + 1, j - 1, id);
                r += f(i + 1, j, id);
                r += f(i + 1, j + 1, id);
                return r;
            }
        };
        int islandId = MIN_ID;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                auto r = f(i, j, islandId);
                if (r > 0) {
                    islandId += 1;
                }
            }
        }

        cout << (islandId - MIN_ID) << endl;
    }
}