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 aruku(int** house,int w,int h,int row,int col){
	if(house[col][row]==0)return;
	house[col][row]=0;
	if(row+1<w)aruku(house,w,h,row+1,col);
	if(row+1<w && col+1<h)aruku(house,w,h,row+1,col+1);
	if(col+1<h)aruku(house,w,h,row,col+1);
	if(row-1>=0 && col+1<h)aruku(house,w,h,row-1,col+1);
	if(row-1>=0)aruku(house,w,h,row-1,col);
	if(row-1>=0 && col-1>=0)aruku(house,w,h,row-1,col-1);
	if(col-1>=0)aruku(house,w,h,row,col-1);
	if(row+1<w && col-1>=0)aruku(house,w,h,row+1,col-1);
return;
}

int tansaku( int** house,int w,int h){
	int row=0;int col=0;int cnt=0;
	for(int i=0;i<h;i++)
	for(int j=0;j<w;j++){
		if(house[i][j]==1){row=j;col=i;cnt++;
			aruku(house,w,h,row,col);
		}
	}
	return cnt;
}


int main(void){
int h,w;
	
	do{
	cin>>w;cin>>h;
	int** house=(int**)calloc(h,sizeof(int*));
	for(int i=0;i<h;i++)house[i]=(int*)calloc(w,sizeof(int));

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){cin>>house[i][j];}
	}
	
	int ret=tansaku(house,w,h);
	if(w!=0 && h!=0)cout<<ret<<endl;
	for(int i=0;i<h;i++)free(house[i]);
	free(house);
	}while(w!=0 && h!=0);
	
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//???

int w,h,field[50][50],ans;

void dfs(int x,int y){
	if(x<0 || x>=w || y<0 || y>=h)return;
	if(field[x][y]==0)return;
	field[x][y]=0;
	dfs(x+1,y);
	dfs(x-1,y);
	dfs(x,y+1);
	dfs(x,y-1);
	dfs(x+1,y+1);
	dfs(x-1,y-1);
	dfs(x-1,y+1);
	dfs(x+1,y-1);
}

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

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll
int w,h;

void f(int c[100][100],bool b[100][100],int y,int x){

  if(y<0||x<0||y>h||x>w||!c[y][x]||b[y][x]) return;
  b[y][x]=true;
  f(c,b,y+1,x);
  f(c,b,y-1,x);
  f(c,b,y,x+1);
  f(c,b,y,x-1);
  f(c,b,y+1,x+1);
  f(c,b,y+1,x-1);
  f(c,b,y-1,x+1);
  f(c,b,y-1,x-1);
}



signed main(){
  while(1){
    
    cin>>w>>h;
    if(w==0) break;

    int c[100][100]={};
    rep(i,h){
      rep(j,w){
        cin>>c[i][j];
      }
    }

    bool b[100][100]={};

    int s=0;

    rep(i,h){
      rep(j,w){
        if(c[i][j]&&!b[i][j]){
          f(c,b,i,j);
          s++;
        }
      }
    }

    cout<<s<<endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

int masu[100][100] = {};
int used[100][100] = {};
int w,h;
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};

void dfs(int x, int y){
    used[x][y] = 1;
    REP(i,8){
        int tx = dx[i] + x;
        int ty = dy[i] + y;
        if(0 <= tx && tx < h && 0 <= ty && ty < w && used[tx][ty] != 1 && masu[tx][ty] == 1){
            dfs(tx,ty);
        }
    }
}

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        REP(i,h) REP(j,w) {
            cin >> masu[i][j];
            used[i][j] = 0;
        }
        int ans = 0;
        REP(i,h)  REP(j,w)  {
            if(!used[i][j] && masu[i][j] == 1){
                dfs(i,j);
                ans++;
            }
        }
        cout << ans << endl;
    }

}