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>
using namespace std;
 
void f(int n){
if(n%100==0){
cerr<<n<<endl;
}
f(n+1);
}
 
int main(){
f(1);
}

C++: #include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

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

typedef struct Point{
    int x;
    int y;
}Point;

bool solve(Point p, int *a){
    
    int res = *a;
    if(c[p.y][p.x] == 1){
        res++;
        c[p.y][p.x] = 0;
        for(int i=-1; i<2; i++){
            for(int j=-1; j<2; j++){
                if(!(i==0 && j==0)){
                    if(0<=p.y+i && p.y+i <h && 0<=p.x+j && p.x+j <w){
                        if(c[p.y+i][p.x+j] == 1){
                        solve(Point{p.x+j,p.y+i}, &res);
                        }
                    }
                }
            }
        }
    }
    
    if(res>0)
    return true;
    
    return false;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    /////////
    //write//
    /////////
    
    while(1){
        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 a = 0;
        int cnt = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(solve(Point{j,i}, &a)){
                    //cout << "x:" << j << ",y:" << i << endl;
                    cnt++;
                }
            }
        }
        
        cout << cnt << endl;
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

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

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

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

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

void dfs(int x,int y){
    C.at(x).at(y) = 0;
    for(int i=-1;i<2;++i){
        for(int j=-1;j<2;++j){
            int nh = x + i;
            int nw = y + j;
            if(0<=nw&&nw<w&&0<=nh&&nh<h&&C.at(nh).at(nw)==1){
            dfs(nh,nw);
            }
        }
        
    }
}

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

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

const int M = 55;
int w,h;
int f[M][M];
int dx[] = {1,1,1,0,0,-1,-1,-1};
int dy[] = {0,1,-1,1,-1,0,1,-1};

void dfs(int x, int y) {
    if(x == w || y == h) return ;
    f[y][x] = 0;
    rep(i,8) {
        int nx = x+dx[i], ny = y+dy[i];
        if(f[ny][nx] == 1)
            dfs(nx, ny);
    }
}

int main() {
    while(cin>>w>>h && w) {
        ZERO(f);
        rep(i,h) rep(j,w) cin >> f[i][j];

        int ans = 0;
        rep(i,h) rep(j,w) {
            if(f[i][j] == 0) continue;
            dfs(j,i);
            ans++;
        }

        cout << ans << endl;
    }

    return 0;
}