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>
#include<utility>
#include<iostream>
#include<queue>

using namespace std;
typedef long long ll;
 
#define REP(i,n) for(int i=0;i<(int)(n);i++)
 
int main()
{
    int ans,w,h;
    int c[55][55];
    queue<pair<int,int> > que;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={-1,0,1,1,1,0,-1,-1};
    while(1==1)
    {
        ans=0;
        REP(i,55)REP(j,55)c[i][j]=10000;
        scanf("%d %d\n",&w,&h);
        if(w==0&&h==0)return 0;
        REP(i,h)REP(j,w)scanf("%d",&c[i+1][j+1]);
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(c[i][j]==1)
                {
                    ans++;
                    c[i][j]=0;
                    while(!que.empty())que.pop();
                    que.push(pair<int,int>(i,j));
                    pair<int,int> p;
                    while(!que.empty())
                    {
                        p=que.front();
                        que.pop();
                        REP(k,8)
                        {
                            if(c[p.first+dy[k]][p.second+dx[k]]==1)
                            {
                                que.push(pair<int,int>(p.first+dy[k],p.second+dx[k]));
                                c[p.first+dy[k]][p.second+dx[k]]=0;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y, int& a);

int main(){

	int w,h,ans;
	
	const int dif = 2;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			return 0;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 2;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(i, j, ans))
					ans++;

		std::cout << ans-dif << std::endl;
	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y, int& a){
	if(field[y][x] != 1)
		return false;

	field[y][x] = a;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j, a);

	return true;
}

C++: #include<iostream>
#include<bitset>

using namespace std;

int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
int w, h, c[50][50];
void dfs(int c[50][50], int x, int y);

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)	break;
		int counter = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}

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

	}
	return 0;
}

void dfs(int c[50][50], int x, int y) {	//(x, y) = 1ということを発見済み
	c[x][y] = 0;
	for (int i = 0; i < 8; i++) {
		if (0 <= x + dx[i] && x + dx[i] < w && 0 <= y + dy[i] && y + dy[i] < h) {
			if (c[x + dx[i]][y + dy[i]] == 1) {
				dfs(c, x + dx[i], y + dy[i]);
			}
		}
	}
}

C++: #include <iostream>

#define W_MAX 55
#define H_MAX 55

int f[H_MAX][W_MAX];

void dfs(int y,int x);


using namespace std;

int main(void)
{
	int w;
	int h;
	int i1,i2;
	int count;
	int a;

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

		//cout << "a";

		for(i1=0;i1<H_MAX;i1++){
			for(i2=0;i2<W_MAX;i1++){
				f[i1][i2]=0;

			}
		}

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


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

		cout << count << endl;

	}
	return 0;
}


void dfs(int y,int x)
{
	int i1,i2;

	f[y][x]=0;
	for(i1=-1;i1<2;i1++){
		for(i2=-1;i2<2;i2++){

			if(f[y+i1][x+i2]==1) dfs(y+i1,x+i2);
		}
	}

	return;
}

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

int w, h, x, y, ans;
int mp[52][52];
int visited[52][52];
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1, 1, 1, -1, 0, 1, -1, 0};


void bfs(int x, int y){
  int i;
  visited[y][x] = 1;
  for(i = 0; i < 8; i++){
    if(visited[y + dy[i]][x + dx[i]] == 0 && mp[y + dy[i]][x + dx[i]] == 1){
      bfs(x + dx[i], y + dy[i]);
    }
  }
}

int main(void){
  while(1){
    cin >> w >> h;
    if(w == 0) break;

    for(y = 0; y < h + 2; y++){
      if(y == 0 || y == h + 1){
        for(x = 0; x < w + 2; x++) mp[y][x] = 0;
      }else{
        for(x = 0; x < w + 2; x++){
          if(x == 0 || x == w + 1){
            mp[y][x] = 0;
          }else{
            cin >> mp[y][x];
          }
        }
      }
    }
    for(y = 0; y < h + 2; y++){
      for(x = 0; x < w + 2; x++){
        visited[y][x] = 0;
      }
    }
    ans = 0;
    for(y = 1; y < h + 1; y++){
      for(x = 1; x < w + 1; x++){
        if(mp[y][x] == 1 && visited[y][x] == 0){
          ans++;
          bfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<vector<int> > imap(52,vector<int>(52,0));
int w,h;

void dfs(int nowx,int nowy){
  imap[nowx][nowy]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(imap[nowx+i][nowy+j]==1) dfs(nowx+i,nowy+j);
    }
  }
}

int main(){
  while(cin>> w>>h&&(w||h)){
    for(int i=1;i<h+1;++i)
      for(int j=1;j<w+1;++j)
	cin >> imap[i][j];

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