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;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int c[100][100];//0　海
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

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

  while(true){
      cin>>w>>h;
      if(w==0&&h==0) break;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          cin>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(i,j);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

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

int w, h;
int c[50][50];

void dfs(int x, int y){
    c[x][y] = 0;
    for(int dx=-1; dx<=1; dx++){
        for(int dy=-1; dy<=1; dy++){
            int nx = x + dx;
            int ny = y + dy;
            if(0<=nx && nx<h && 0<=ny && ny<w && c[nx][ny]==1) dfs(nx, ny);
        }
    }
    return;
}

int main(){
  	cin >> w >> h;
    while(w!=0){
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> c[i][j];
            }
        }
        int ans = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(c[i][j]==1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
      	cin >> w >> h;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;
typedef long long ll;

int ans = 0;
vector <vector<int> > MAP;

int solve(int locateX,int locateY,int w,int h){
  //!
  if(locateX<0 || locateY<0 || locateX>=h || locateY>=w)return 0;
  //CHECK
  if(MAP[locateX][locateY]==1){
    MAP[locateX][locateY]=0;
    //!--
    solve(locateX,locateY+1,w,h);
    solve(locateX+1,locateY+1,w,h);
    solve(locateX+1,locateY,w,h);
    solve(locateX+1,locateY-1,w,h);
    solve(locateX,locateY-1,w,h);
    solve(locateX-1,locateY-1,w,h);
    solve(locateX-1,locateY,w,h);
    solve(locateX-1,locateY+1,w,h);
    //--!
    return 1;
  }
  return 0;
}

int ope(){
  int w,h;
  ans=0;
  MAP.resize(0);
  cin >> w >> h;
  if(!w&&!h)return 0;
  //INPUT
  for(int i=0;i<h;++i){
    vector<int> temp;
    for(int j=0;j<w;++j){
      int ti;
      cin >> ti;
      temp.push_back(ti);
    }
    MAP.push_back(temp);
  }
  //EXEC
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j)  
      ans+=solve(i,j,w,h);

  //OUTPUT
  cout << ans << endl;
  return 1;
}
int main(){
  while(ope()){}
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

using Graph = vector<vector<int>>;
//using Graph = vector<vector<Edge>>;
bool seen[100][100];
int field[100][100];

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

int w, h;

void dfs(int i, int j){
    seen[i][j] = true;
    REP(k, 8){
        if(i+dx[k] >= h || i+dx[k] < 0 || j+dy[k] >= w || j+dy[k] < 0) continue;
        if(field[i+dx[k]][j+dy[k]] == 0) continue;
        if(seen[i+dx[k]][j+dy[k]]) continue;
        dfs(i+dx[k], j+dy[k]);
    }
}

int main()
{
    for(;;){
        cin >> w >> h;
        if(w==0 && h==0)
            break;
        REP(i, h)
            REP(j, w){
                cin >> field[i][j];
                seen[i][j] = false;
            }
        int count = 0;
        REP(i, h)
            REP(j, w){
                if(seen[i][j] || field[i][j] == 0) continue;
                dfs(i, j);
                count++;
            }
        cout << count << endl;
    }

    return 0;
    
}

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

using namespace std;

using Point = pair<int,int>;

int map[50][50];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;
    map[y][x] = 0;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
        }
      }
    }
  }

}

int main(){
  int w,h;
  while(cin >> w >> h&&w>0&&h>0){
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        cin >> map[i][j];
      }
    }

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}