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 <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
int c[50][50];
int vy[8] = {-1,-1,0,1,1,1,0,-1};
int vx[8] = {0,1,1,1,0,-1,-1,-1};
void calc(int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return;
  if(c[y][x] == 0) return;
  c[y][x] = 0;
  FOR(i,0,8) calc(y+vy[i],x+vx[i]);
  return;
}
int main()
{
  while(cin >> W >> H, (W || H)) {
    FOR(y,0,H) FOR(x,0,W) cin >> c[y][x];
    int ans = 0;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(c[y][x] == 0) continue;
        calc(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
#define MAX_N 50

int field[MAX_N][MAX_N];

void init(){
	rep(i,MAX_N){
		rep(j,MAX_N){
			field[i][j] = 0;
		}
	}
}

void rec(int x,int y,int maxW,int maxH){
	if(field[y][x] == 0){
		return;
	}
	field[y][x] = 0;
	int dx[8] = {-1,0,1,-1,1,-1,0,1};
	int dy[8] = {-1,-1,-1,0,0,1,1,1};
	rep(i,8){
		int nx,ny;
		nx = x + dx[i];
		ny = y + dy[i];
		if( 0 <= nx && nx < maxW && 0 <= ny && ny < maxH){
			rec(nx,ny,maxW,maxH);
		}
	}
	return;
}

int main()
{
	int w,h;
	cin>>w>>h;
	while( !((w == 0) && (h == 0))){
		init();
		rep(i,h){
			rep(j,w){
				cin>>field[i][j];
			}
		}
		int ans = 0;
		rep(i,h){
			rep(j,w){
				if(field[i][j] == 1){
					ans++;
					rec(j,i,w,h);
				}
			}
		}
		cout<<ans<<endl;
		cin>>w>>h;
	}

	return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						int tmp1,tmp2,tmp3,tmp4;
						tmp1=a[i][j-1];
						tmp2=a[i-1][j-1];
						tmp3=a[i-1][j];
						tmp4=a[i-1][j+1];
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp1)a[k][l]=tmp;}}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp2)a[k][l]=tmp;}}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp3)a[k][l]=tmp;}}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp){REP(k,w){REP(l,h){if(a[k][l]==tmp4)a[k][l]=tmp;}}}
					}
				}
			}
		}
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}

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

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout << is << endl;
    cin >> w >> h;
  }
  return 0;
}

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

C++: #include <iostream>

using namespace std;

const int MAX_LENGTH = 50;

int map[MAX_LENGTH][MAX_LENGTH];

void dfs(int x, int y) {
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (y + i >= 0 && MAX_LENGTH > y + i && x + j >= 0 && MAX_LENGTH > x + j && map[y+i][x+j] == 1) {
				map[y+i][x+j] = 0;
				dfs(x + j, y + i);
			}
		}
	}

	return;
}

int main() {
	while (true) {
		int w, h, count;
		cin >> w >> h;
		if (w == 0) {
			break;
		}
		for (int i = 0; i < MAX_LENGTH; i++) {
			for (int j = 0; j < MAX_LENGTH; j++) {
				map[i][j] = 0;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					dfs(j, i);
					count++;
					/*
					for (int k = 0; k < h; k++) {
						for (int l = 0; l < w; l++) {
							cout << map[k][l] << " ";
						}
						cout << endl;
					}
					cout << endl;
					*/
				}
			}
		}
		cout << count << endl;
	}

	return 0;
}