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<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

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

int w, h;
VII cs;

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

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1){
		cs.clear();
		int ans = 0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		rep(i, h){
			VI c;
			rep(j, w){
				int a;
				cin >> a;
				c.pb(a);
			}
			cs.pb(c);
		}
		rep(i, cs.size()){
			rep(j, cs[i].size()){
				if(!cs[i][j]) continue;
				dfs(i, j);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

#define PI 3.141592653
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}

vector<vector<bool>> visited;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
int n,m;
int dfs(int x,int y,vector<vector<int>> field){
    if(visited[x][y])return 0;
    visited[x][y]=true;
    int ret=1;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||ny<0||nx>=n||ny>=m)continue;
        if(visited[nx][ny]==false&&field[nx][ny]){
            ret=dfs(nx,ny,field);
        }
    }
    return ret;
}
int main(void){
    vector<int> ans;
    while(1){
        cin>>m>>n;
        if(n==0&&m==0)break;
        vector<vector<int>> field(n,vector<int>(m));
        rep(i,0,n){
            rep(j,0,m){
                cin>>field[i][j];
            }
        }
        visited=vector<vector<bool>>(n,vector<bool>(m));
        int ret=0;
        rep(i,0,n){
            rep(j,0,m){
                if(field[i][j]){
                    ret += dfs(i,j,field);
                }
            }
        }
        ans.push_back(ret);
    }
    for(auto i:ans){
        cout<<i<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
int m[52][52];
void F(int x,int y)
{
	if(m[y][x])
	{
		m[y][x]=0;
		int i,j;
		for(i=-1;i<2;++i)for(j=-1;j<2;++j)F(x+i,y+j);
	}
}
int main()
{
	int h,w,i,j,r;
	while(scanf("%d%d",&w,&h),w)
	{
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)scanf("%d",&m[i][j]);
		for(i=0;i<h+2;++i)m[i][w+1]=0;
		for(r=j=0;j<w+2;++j)m[h+1][j]=0;
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)if(m[i][j])F(j,i),++r;
		printf("%d\n",r);
	}
	return 0;
}

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

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define dis distance
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector <int> > vvi;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9+7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);


typedef pair<int,bool> P;

int w,h;

bool check(int y, int x){
    if(y < 0 || y >= h)return false;
    if(x < 0 || x >= w)return false;
    return true;
}

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

void dfs(int y, int x, vvb &V){
    V[y][x] = false;
    REP(i,8){
        if(check(y + dy[i],x + dx[i]) && V[y + dy[i]][x + dx[i]]){
            dfs(y + dy[i], x + dx[i], V);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> w >> h;
    vi ansV;
    while(w != 0 && h != 0){
        int ans = 0;
        vvb V(h,vb(w,false));
        REP(i,h){
            REP(j,w){
                int a;
                cin >> a;
                if(a == 1)V[i][j] = true;
            }
        }
        REP(i,h){
            REP(j,w){
                if(V[i][j]){
                    dfs(i,j,V);
                    ans++;
                }
            }
        }
        ansV.push_back(ans);
        cin >> w >> h;
    }
    for(int a: ansV){
        cout << a << endl;
    }
    return 0;
}