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>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m)  vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << var << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << var;
#define prt2(v1, v2)     cout << v1 << " " << v2 << "\n"
#define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "yes" : "no");}
//----------------------------------------------------------------
bool ch[51][51];
int h, w;

void dfs(vector<vector<bool>> mp, int i, int j){
  //prt3("--->", i, j);
  ch[i][j] = true;
  bool up=true, down=true, left=true, right=true;

  if(i==0) up=false;
  if(i==h-1) down=false;
  if(j==0) left=false;
  if(j==w-1) right=false;

  if(up && left){if(mp[i-1][j-1] && !ch[i-1][j-1]) dfs(mp, i-1, j-1);}
  if(up) {if(mp[i-1][j] && !ch[i-1][j]) dfs(mp, i-1, j);}
  if(up && right) {if(mp[i-1][j+1] && !ch[i-1][j+1]) dfs(mp, i-1, j+1);}
  if(left) {if(mp[i][j-1] && !ch[i][j-1]) dfs(mp, i, j-1);}
  if(right) {if(mp[i][j+1] && !ch[i][j+1]) dfs(mp, i, j+1);}
  if(down && left) {if(mp[i+1][j-1] && !ch[i+1][j-1]) dfs(mp, i+1, j-1);}
  if(down) {if(mp[i+1][j] && !ch[i+1][j]) dfs(mp, i+1, j);}
  if(down && right) {if(mp[i+1][j+1] && !ch[i+1][j+1]) dfs(mp, i+1, j+1);}
}

int main(void) {
  while(1){
    rep(i,51){
      rep(j,51){
        ch[i][j]=false;
      }
    }
    cin >> w >> h;

    if(w==0 && h==0){break;}
    vector<vector<bool>> mp(h, vector<bool>(w, false));
    rep(i,h){
      rep(j,w){
        int tmp;
        cin >> tmp;
        mp[i][j] = (tmp>0 ? true : false);
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
        if(!ch[i][j] && mp[i][j]){
          ans++;
          dfs(mp, i, j);
        }
      }
    }
    prt(ans);
  }
}

C++: #include <iostream>

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,ny= y+dy;
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny] == 1){
                dfs(nx,ny);
            }
        }
    }
    return;
}

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

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repp(i, a, b) for(int i = a; i <= b; i++)
using namespace std;
using ll = long long;
using vel = vector<ll>;
using vvel = vector<vel>;

vvel maze;
ll w, h;

ll dfs(ll y, ll x) {
    if(maze[y][x]!=1) return 0;
    maze[y][x] = -1;
    repp(dy, -1, 1) {
        repp(dx, -1, 1) {
            if(y+dy < 0 || y+dy >= h || x+dx < 0 || x+dx >= w ) continue;
            if(dx == 0 && dy == 0) continue;
            dfs(y+dy, x+dx);
        }
    }
    return 1;
};

int main() {
    while(cin >> w >> h, w) {
        maze.assign(h, vel(w, 0));
        rep(y, h) rep(x, w) cin >> maze[y][x];
        ll ans = 0;
        rep(y, h) {
            rep(x, w) {
                if(maze[y][x] == 1) 
                    ans += dfs(y, x);
            }
        }
        cout << ans << endl;
    }
}

C++: // AOJ 1160
#include<iostream>
#define rep(i,a) for( int i = 0; i != (a); ++i )

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

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

void dfs( int y, int x )
{
	c[y][x] = false;

	rep( i, 8 )
	{
		int nx = x+dx[i], ny = y+dy[i];

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

	return;
}

int main()
{
	while( std::cin >> w >> h, w|h )
	{
		rep( i, h )
			rep( j, w )
				std::cin >> c[i][j];

		int ans = 0;
		rep( i, h )
			rep( j, w )
				if( c[i][j] )
					dfs( i, j ), ++ans;

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

	return 0;
}