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 rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<int,int> P;
typedef set<int> S;
ll n;
int maze[55][55];
int attac[55][55];
int dx[] = {1,-1,0,0,1,-1,1,-1};
int dy[] = {0,0,1,-1,1,1,-1,-1};
int w,h;
void bfs(int i,int j){
    queue<P> que;
    que.push({i,j});
    //cout<<"111"<<endl;
    while(!que.empty()){
        P now;
        now = que.front();
        que.pop();
        int x = now.first;
        int y = now.second;
        rep(k,8){
            //cout<<"aaa"<<endl;
            int nx = x + dx[k];
            int ny = y + dy[k];
            if(nx<h && nx>=0 && ny<w && ny>=0 && attac[nx][ny]==0 && maze[nx][ny]==1){
                //cout<<"ああ"<<endl;
                attac[nx][ny]=1;
                que.push({nx,ny});
            }
        }
    }
}
int main(){
    while (1){
        int ans=0;
        cin>>w>>h;
        if(w==0 && h==0) break;
        rep(i,55){
            rep(j,55) attac[i][j]=0;
        }
        rep(i,h){
            rep(j,w) cin>>maze[i][j];
        }
        rep(i,h){
            rep(j,w){
                if(maze[i][j]==1 && attac[i][j]==0){
                    ans++;
                    bfs(i,j);
                }
            }
        }
        cout<<ans<<endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef vector<int>    VI;
typedef vector<vector<int>>    VVI;
typedef vector<bool>   VB;
typedef vector<string> VS;

#define MP make_pair
#define PB push_back
#define ALL(x)  (x).begin(),(x).end()
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define RANGE(x, min, max) min <= x && x <= max

const int DX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int DY[] = {-1, -1, -1, 0, 0, 1, 1, 1};

int w, h;
VVI cs;

void dfs(int y, int x) {
	cs[y][x] = 0;
	rep (i, 8) {
		int ny = y + DY[i];
		int nx = x + DX[i];
		if (RANGE(nx, 0, w - 1) && RANGE(ny, 0, h - 1) && cs[ny][nx]) {
			dfs(ny, nx);
		}
	}
}

int main(void) {
	while (1) {
		cs.clear();
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		cs.resize(h, VI (w));
		rep (i, h) rep (j, w) {
			cin >> cs[i][j];
		}
		int ans = 0;
		rep (i, h) rep (j, w) {
			if (!cs[i][j]) continue;
			dfs(i, j);
			ans++;
		}
		cout << ans << endl;
	}
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

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

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

int main(){
 
	while(cin>>w>>h,w||h){
		 FOR(i,h) FOR(j,w) cin>>s[i][j];
		 int ans = 0;
		 FOR(i,h) FOR(j,w){
			 if(s[i][j]){
				 dfs(i,j);
				 ans++;
			 }
		 }
		 cout << ans << endl;
	}
    return 0;
}

C++: // A0J 1160 How Many Islands?

#include <iostream>

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

using namespace std;

int map[51][51];
int moumita[51][51];
int w,h;

bool check(int j, int k){
	if (j>=0 && j<h && k>=0 && k<w && map[j][k]==1 && !moumita[j][k]) return true;
	else return false;
}

int dfs(int j, int k){
	moumita[j][k] = true;
	
	if (check(j-1,k-1)) dfs(j-1,k-1);
	if (check(j-1,k)) dfs(j-1,k);
	if (check(j-1,k+1)) dfs(j-1,k+1);
	if (check(j,k-1)) dfs(j,k-1);
	if (check(j,k+1)) dfs(j,k+1);
	if (check(j+1,k-1)) dfs(j+1,k-1);
	if (check(j+1,k)) dfs(j+1,k);
	if (check(j+1,k+1)) dfs(j+1,k+1);
	
	return 1;
}

int main(){
	
	int ans;
	
	ans = 0;
	
	cin >> w >> h;
	
	REP(j,h){
		REP(k,w){
			cin >> map[j][k];
			moumita[j][k] = false;
		}
	}
	
	REP(j,h){
		REP(k,w){
			if (check(j,k))	ans += dfs(j,k);
		}
	}
	
	cout << ans << endl;
	
	return 0;
	
}

C++: // 2009à\IB Í¢­Â éH
#include <iostream>

using namespace std;

int w, h;
int c[50][50];
// 8ßTÖÌÚ®ðè`
char dx[] = {-1,-1,-1,0,1,1,1,0};
char dy[] = {-1,0,1,1,1,0,-1,-1};

// ÄAÅ»ÝÌ¤©çBÅ«é¤ðÁµÁÄ¢­
void dfs(int x, int y){
	c[x][y] = 0;
	for(int i=0;i<8;i++){
		int nx = x+dx[i], ny = y+dy[i];
		// (nx,ny)ªtB[hàÉ é©ÆC»±ª¤©ð»è
		if(nx<0||h<=nx||ny<0||w<=ny||!c[nx][ny]) continue;
		dfs(nx,ny);
	}
}

int main(){
	while(cin >> w >> h, w){
		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]) ans++, dfs(i,j);
		cout << ans << endl;
	}
}