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 <queue>
#include <utility>
#include <vector>
using namespace std;

int main() {
	int w, h;
	while (cin >> w >> h, h || w) {
		vector<vector<int>> c(h, vector<int>(w));
		for (vector<int>& i : c) {
			for (int& j : i) cin >> j;
		}
		int ans = 0;
		vector<vector<bool>> a(h, vector<bool>(w));
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (!a[i][j] && c[i][j]) {
					++ans;
					a[i][j] = true;
					queue<pair<int, int>> b;
					b.emplace(i, j);
					while (!b.empty()) {
						if (b.front().first) {
							if (!a[b.front().first - 1][b.front().second] && c[b.front().first - 1][b.front().second]) {
								a[b.front().first - 1][b.front().second] = true;
								b.emplace(b.front().first - 1, b.front().second);
							}
							if (b.front().second) {
								if (!a[b.front().first - 1][b.front().second - 1] && c[b.front().first - 1][b.front().second - 1]) {
									a[b.front().first - 1][b.front().second - 1] = true;
									b.emplace(b.front().first - 1, b.front().second - 1);
								}
							}
							if (b.front().second != w - 1) {
								if (!a[b.front().first - 1][1 + b.front().second] && c[b.front().first - 1][1 + b.front().second]) {
									a[b.front().first - 1][1 + b.front().second] = true;
									b.emplace(b.front().first - 1, 1 + b.front().second);
								}
							}
						}
						if (b.front().first != h - 1) {
							if (!a[1 + b.front().first][b.front().second] && c[1 + b.front().first][b.front().second]) {
								a[1 + b.front().first][b.front().second] = true;
								b.emplace(1 + b.front().first, b.front().second);
							}
							if (b.front().second != w - 1) {
								if (!a[1 + b.front().first][1 + b.front().second] && c[1 + b.front().first][1 + b.front().second]) {
									a[1 + b.front().first][1 + b.front().second] = true;
									b.emplace(1 + b.front().first, 1 + b.front().second);
								}
							}
						}
						if (b.front().second) {
							if (!a[b.front().first][b.front().second - 1] && c[b.front().first][b.front().second - 1]) {
								a[b.front().first][b.front().second - 1] = true;
								b.emplace(b.front().first, b.front().second - 1);
							}
							if (b.front().first != h - 1) {
								if (!a[1 + b.front().first][b.front().second - 1] && c[1 + b.front().first][b.front().second - 1]) {
									a[1 + b.front().first][b.front().second - 1] = true;
									b.emplace(1 + b.front().first, b.front().second - 1);
								}
							}
						}
						if (b.front().second != w - 1) {
							if (!a[b.front().first][1 + b.front().second] && c[b.front().first][1 + b.front().second]) {
								a[b.front().first][1 + b.front().second] = true;
								b.emplace(b.front().first, 1 + b.front().second);
							}
						}
						b.pop();
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

using namespace std;

queue<int> Q;
int A[52][52];
int UP[8]  ={-1, -1, 1, 0, 1, 1, 1, 0};
int SIDE[8]={-1, 0, 1, 1, 1, 0, -1, -1};


void route(int w,int h){

  A[w][h]=0;
  if(A[w+1][h+1]==1)route(w+1,h+1);
  if(A[w+1][h]==1)route(w+1,h);
  if(A[w+1][h-1]==1)route(w+1,h-1);
  if(A[w][h+1]==1)route(w,h+1);
  if(A[w][h-1]==1)route(w,h-1);
  if(A[w-1][h+1]==1)route(w-1,h+1);
  if(A[w-1][h]==1)route(w-1,h);
  if(A[w-1][h-1]==1)route(w-1,h-1);


}

int main(){
  int w,h;
  int count;
  while(true){
    cin>>w>>h;
    if(w==0&&h==0)break;
    for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	cin>>A[i][j];
     
      }
    }
    count=0;
    for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	if(A[i][j]==1){
	  //	  cout<<i<<" "<<j<<endl; 
	  route(i,j);
	  count++;
	}
      }
    }

    //デバック
   
    /*     for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	cout<<" "<<A[i][j];
      }
      cout <<endl;
      }*/
    
    cout<<count<<endl;
   }
  return 0;

}

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

using namespace std;


#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

int h,w;

int c[52][52];

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


void dfs(int i,int j);



int main(){


	while(1){
		cin >> w >> h;

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

		cnt=0;

		REP(i,h){
			REP(j,w){


				cin >> c[i][j] ;

			}
		}

		REP(i,h){
			REP(j,w){

				//cnt++??????????????\???????????????????????\???????????????
				if(c[i][j] == 1){
					cnt++;
					dfs(j,i);
				}
			}
		}

		cout << cnt << endl;
	}
	return 0;
}

void dfs(int i,int j){

	if(c[j][i] == 1){

		c[j][i] = 0;
		REP(k,8){
			//w???<=???????????£??????????????????
			if(0 <= i+dx[k] && i+dx[k] < w && 0 <= j+dy[k] && j+dy[k] < h){

				dfs(i+dx[k],j+dy[k]);
			}
		}
	}
}

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

#define REP(i, n) for(int i = 0; i < n; i++)

const int MAX_N = 50;
int w, h; // wは横の個数 hは縦の個数
int c[MAX_N][MAX_N]; //マップ
int dx[] = { 1, -1, 0 }, dy[] = { 1, -1, 0 }; //移動単位
int island; //記憶

void sch(int x, int y) { //ひとつ前のx座標とy座標

	c[x][y] = 0;
	REP(i, 3) {
		REP(j, 3) {
			int xx = x + dx[i];
			int yy = y + dy[j];
			if (xx < h && xx >= 0 && yy < w && yy >= 0 && c[xx][yy] == 1) {
				sch(xx, yy);
			}

		}
	}
	return;
}

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

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

		REP(i, h) {
			REP(j, w) cin >> c[i][j];
		}

		REP(i, h) {
			REP(j, w) if (c[i][j] == 1) {
				sch(i, j);
				island++;
			}
		}

		cout << island << "\n";
	}
	return 0;
}