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>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9+7)
#define i_5 (ll)(1E9+5)
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
ll inf=(ll)1E12;//10^12
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
const double EPS=1E-8;
////////////////////////////////////////

int w,h;
bool m[55][55];

void dfs(int k,int l){
    
    rep(i,-1,1)rep(j,-1,1){
        int x=k+i;int y=l+j;
        if(0<=x&&x<=h-1&&0<=y&&y<=w-1&&m[x][y]){
            
            m[x][y]=false;
            dfs(x,y);
        }
    }
}


int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    
    while(cin>>w>>h){
        if(w==0)break;
        rep(i,0,h-1)rep(j,0,w-1)cin>>m[i][j];
        int ans=0;
        rep(i,0,h-1)rep(j,0,w-1){
            if(m[i][j]){
                ans++;
                m[i][j]=false;
                dfs(i,j);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int grid[100][100];
int H, W;

void dfs(int x, int y) {
  int mx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  int my[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  for (int i = 0; i < 8; i++) {
    int _x = x + mx[i];
    int _y = y + my[i];
    if (grid[_x][_y] == 1 && _x >= 0 && _y >= 0 && _x < W && _y < H) {
      grid[_x][_y] = 0;
      dfs(_x, _y);
    }
  }
}

signed main() {
  while (cin >> W >> H, W+H!=0) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }
 
    int ans = 0; 
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (grid[j][i] == 1) {
          dfs(j,i);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int m[50][50];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

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

int w,h;
int g[100][100];

void dfs(int y, int x){
  g[y][x] = 0;

  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if(y+i<0 || x+j<0 || y+i>=h || x+j>=w)continue;
      if(g[y+i][x+j])dfs(y+i,x+j);
    }
  }
}

int main(){
  while(cin >> w >> h, w){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++)cin >> g[i][j];
    }

    int cnt = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(g[i][j])cnt++, dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
}

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

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

int w, h;
int C[50][50] = {};

void visit(int x, int y) {
  C[x][y] = 0;
  for (int ix = x - 1; ix <= x + 1; ix++) {
    for (int iy = y - 1; iy <= y + 1; iy++) {
      if (ix >= 0 && iy >= 0 && ix < w && iy < h && C[ix][iy] == 1) {
        visit(ix, iy);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w) {
    int ans = 0;
    C[50][50] = {};
    REP(j, h) {
      REP(i, w) { cin >> C[i][j]; }
    }
    REP(i, w) {
      REP(j, h) {
        if (C[i][j] == 1) {
          ans++;
          visit(i, j);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int W, H;
// dfs
// https://qiita.com/drken/items/4a7869c5e304883f539b
void dfs(vector<vector<int>> &g, int h, int w) {
	g[h][w] = 0;
	for(int dh = -1; dh <= 1; dh++) {
		for(int dw = -1; dw <= 1; dw++) {
			int next_h = h + dh;
			int next_w = w + dw;
			if(next_h < 0 || next_h >= H || next_w < 0 || next_w >= W) continue;
			if(g[next_h][next_w] == 0) continue;
			dfs(g, next_h, next_w);
		}
	}
}

int main() {
	while(1) {
		cin >> W >> H;
		if(W == 0 && H == 0) break;
		vector<vector<int>> c(H, vector<int>(W));
		rep(i, H) rep(j, W) {
			cin >> c[i][j];
		}
		int cnt = 0;
		rep(i, H) rep(j, W) {
			if(c[i][j] == 0) continue;
			dfs(c, i, j);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	//f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	//printf("%d\n",a);
	//main();
}