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>
#define pq priority_queue
using namespace std; typedef pair<int, int> P; typedef pair<int, P> P2;
typedef pair<int, P2> P3; typedef long long ll; typedef long double ld;
constexpr long long gcd(long long a, long long b){return b ? gcd(b, a % b) : a;}
constexpr long long lcm(long long a, long long b){return a / gcd(a, b) * b;}
constexpr int INF = 1e9, MOD = INF + 7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
constexpr int mod_pow(long long x, long long n, const int mod){long long ret=1;while(n){if(n&1)(ret*=x)%=mod;(x*=x)%=mod;n>>=1;}return ret;}
template<int n> struct Prime{bool arr[n+1];constexpr bool operator[](int k){return arr[k];}constexpr Prime():arr(){for(int i=2;i<n;i++){arr[i]=true;for(int j=2;j*j<=i;j++){if(!(i%j))arr[i]=false;}}}};
template<int n> struct Factorial{long long arr[n+1],ary[n+1];constexpr Factorial():arr(),ary(){arr[0]=1;ary[0]=1;for(int i=0;i<n;i++){arr[i+1]=arr[i]*(i+1)%MOD;ary[i+1]=mod_pow(arr[i+1],MOD-2,MOD);}}};
constexpr Factorial<10> fact; constexpr Prime<10> prime;
constexpr int comb(int a, int b){long long pos = fact.arr[a], pot = fact.ary[a - b], por = fact.ary[b];return pos * pot % MOD * por % MOD;}
template<int n> struct Bernoulli{long long arr[n+1];constexpr Bernoulli():arr(){arr[0]=1;for(int i=1;i<=n;i++){long long sum=0;for(int j=0;j<i;j++){(sum+=comb(i+1,j)*arr[j]%MOD)%=MOD;}arr[i]=(MOD-mod_pow(i+1,MOD-2,MOD))%MOD*sum%MOD;}}};
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
constexpr int sqrtN = 512, logN = 32;
constexpr ld PI = abs(acos(-1));
constexpr ll LINF=1e18;

int w, h, a[55][55];

int main(){
	while(cin >> w >> h,w){
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> a[i][j];
			}
		}
		
		int c = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(a[i][j] != 1) continue; c++;
				
				queue<P> qu; qu.push({i, j}); a[i][j] = 2;
				while(!qu.empty()){
					auto p = qu.front(); qu.pop();
					
					for(int k = 0; k < 8;){
						int nx = p.first + around[k], ny = p.second + around[++k];
						if(nx<0||ny<0||nx>=h||ny>=w||a[nx][ny] != 1) continue;
						a[nx][ny] = 2;
						qu.push({nx, ny});
					}
				}
			}
		}
		
		cout << c << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;
int w = 51,h = 51;
vector<vector<int>> maze(h,vector<int>(w,0));
vector<int> dx = {0,0,1,-1,1,1,-1,-1};
vector<int> dy = {1,-1,0,0,1,-1,1,-1};

void dfs(int y,int x){
        maze.at(y).at(x) = 2;
        rep(i,8){
                int new_y,new_x;
                new_y = y + dy[i];
                new_x = x + dx[i];
                if(0 <=new_y && new_y < h && 0<= new_x && new_x < w){
                        if(maze.at(new_y).at(new_x) == 1){
                                dfs(new_y,new_x);
                        }
                }
        }
}

int main(){
        while(1){
                cin >> w >> h;
                if(w == 0 && h == 0) break;
            rep(i,h)rep(j,w) cin >> maze.at(i).at(j);
        
            int cnt = 0;
            rep(i,h)rep(j,w){
                        if(maze.at(i).at(j) == 1){
                                dfs(i,j);
                        cnt ++;
                }
            }
        
                cout << cnt << endl;
        }
        return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y);

int main(){

	int w,h,ans;
	
	while(true){

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

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

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

		std::cout << ans << 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){
	if(field[y][x] == 0)
		return false;

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

	return true;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
vector< vector<int> > c(50, vector<int> (50));
void dfs(int x, int y, int w, int h){
  c[y][x] = 0;

  for(int i = -1 ; i <= 1 ; i++){
    for(int j = -1 ; j <= 1 ; j++){
      int nx = x + i, ny = y + j;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1)
        dfs(nx, ny, w, h);
    }
  }
}

int main(void){

while(true){
  int w, h;   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 ans = 0;
  for(int i = 0 ; i < h ; i++){
    for(int j = 0 ; j < w ; j++){
      if(c[i][j] == 1){
        dfs(j, i, w, h);
        ans++;
      }
    }
  }

  cout << ans << endl;
}

  return 0;
}

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

int field[50][50];
int w, h;
vector<int> di_x = {1,0,-1,0,1,-1,1,-1};
vector<int> di_y = {0,1,0,-1,1,1,-1,-1};

void dfs(int x, int y) {
   field[x][y] = 0;
   rep(i,8) {
      int n_x = x + di_x[i];
      int n_y = y + di_y[i];
      if (n_x < 0 || h <= n_x || n_y < 0 || w <= n_y) continue;
      else if (field[n_x][n_y] == 0) continue;
      else dfs(n_x,n_y);
   }
}

int main () {
   while (true) {
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      int ct = 0;
      rep(i,h)rep(j,w) cin >> field[i][j];
      rep(i,h)rep(j,w) {
         if (field[i][j] == 1) {
            dfs(i,j);
            ct++;
         }
      }
      cout << ct << endl;
      rep(i,h)rep(j,w) field[i][j] = 0;
   }
}