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++: #define _GLIBCXX_DEBUG

#include"bits/stdc++.h"
using namespace std;

//loops
#define REP(i,n) for(int i=0;i<(ll)(n);i++)
#define REPD(i,n) for(int i=(ll)(n)-1;i>=0;i--)
#define OneToN(i,n) for(int i=1;i<(ll)(n+1);i++)
#define ZeroToN(i,n) for(int i=0;i<(ll)(n+1);i++)
#define AToB(i,a,b) for(int i=a;i<(ll)(b+1);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)

//bitsearch
#define BITSEARCH(bit, n) for (int bit = 0; bit < (1<<n); ++bit)
#define isOne(bit, i) bit & (1<<i)


//arrays
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define add push_back


#define remove_unique(v) v.erase(unique(ALL(v)), v.end())

//pair
#define fir first
#define sec second

//types
#define ll long long
#define vec vector
#define cord2d pair<int,int>
//UNCOMMENT WHEN NEEDED
//#define int ll

//input
template <class T> T get(){ T s; cin >> s; return(s);}
#define gi get<int>()
#define gs get<string>()

template <class T> vector<T> getv(int n) { vec<T> v(n); REP(i, n) cin >> v[i]; return v; }
#define gim(n) getv<int>(n)
#define gsm(n) getv<string>(n)

//output
void print(int a){ cout << a << endl; }
void print(long long a){ cout << a << endl; }
void print(string a){ cout << a << endl; }
void print(char a){ cout << a << endl; }
void print(double a){ cout << a << endl; }
void print(double a, int dig){ cout << std::setprecision(dig) << a << endl; }

template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

template <class T = int>
void print(vec<T> v){ cout << v << endl; }

template <class T = int>
void print2dVec(vec<vec<T>> v2d){for(vec<T> v: v2d) {print(v);}}

void YesNo1(bool i = true){ return print(i?"Yes":"No"); }
void YESNO2(bool i = true){ return print(i?"YES":"NO"); }

//debug output
#define var_name(var) #var
template <class T> void debug(T &var, int nest = 0, string before = "") { cout << string("          ", nest) << before; print(var); }

//name replacement
#define y0 y0__
#define y1 y1__
#define j0 j0__
#define j1 j1__

//bool lambdas
#define lamb(exp) [](auto i){return exp;}
#define isEven [](int i){return i % 2 == 0;}
#define isOdd [](int i){return i % 2 != 0;}

//constants
const ll INF = 1e18;
const int MOD = 10000007;

//maths
int factorial(int k){ int sum = 1; for (int i = 1; i <= k; ++i) { sum *= i; } return sum; }
int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

////////////////////

// 深さ優先探索

vec<vec<bool>> seen;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};

int w,h;

void dfs(vec<vec<int>> &grid, int x, int y) {
    //cout << "   dfs - " << x << y << endl;
    seen[y][x] = true;
    if (grid[y][x] == 0) return;

    REP(i, 8) {
        int newX = x + dx[i];
        int newY = y + dy[i];
        //cout << newX << newY << ", ";

        if (newX < 0 || newX >= w || newY < 0 || newY >= h) continue;
        if (seen[newY][newX]) continue;
        if (grid[newY][newX] == 0) {
            //seen[newY][newX] = true;
            continue;
        }
        dfs(grid, newX, newY);
    }
    //cout << endl;
}

void Main() {
    //code here
    vec<int> answers;
    while(true) {
        w = gi;
        h = gi;
        //print(w);
        //print(h);
        //print("gotgrid");
        if (w==0 && h==0) {
            //print("hoo");
            break;
        }

        vec<vec<int>> grid;
        seen = {};
        REP(i, h) {
            grid.add(gim(w));
            seen.add({});
            REP(j, w) {
                seen[i].add(false);
            }
        }
        //print(grid);
        int res = 0;
        REP(y, h) {
            REP(x, w) {
                //print(" checknext");
                if (grid[y][x] == 1 && seen[y][x] == false) {
                    //print("  gonewisland");
                    dfs(grid, x, y);
                    res += 1;
                }
            }
        }
        answers.add(res);
    }
    for(int ans: answers) {
        print(ans);
    }
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Main();
}

C++: #include <iostream>
#include <vector>

using namespace std;

int dh[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dw[] = {0, 1, 1, 1, 0, -1, -1, -1};

void dfs(int h, int w, int **c, int sh, int sw, int **seen, int t) {
    seen[sh][sw] = t;
    
    for (int i = 0; i < 8; i++) {
        int nh = sh + dh[i], nw = sw + dw[i];
        if (nh < 0 || h <= nh || nw < 0 || w <= nw || seen[nh][nw] != -1 || c[nh][nw] == 0)
            continue;
        
        dfs(h, w, c, nh, nw, seen, t);
    }
}

void solve(int h, int w) {
    int **c = new int*[h];
    int **seen = new int*[h];
    for (int i = 0; i < h; i++) {
        c[i] = new int[w];
        seen[i] = new int[w];
        for (int j = 0; j < w; j++) {
            cin >> c[i][j];
            seen[i][j] = -1;
        }
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (seen[i][j] == -1 && c[i][j] != 0) {
                dfs(h, w, c, i, j, seen, ans+1);
                ans++;
            }
        }
    }

    cout << ans << endl;
    
}

int main() {
    int h, w;
    while (1) {
        cin >> h >> w;
        if (h == 0 && w == 0)
            break;
        
        solve(w, h);
    }
    
    return 0;
}