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 int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return (lx<=x&&x<ux&&ly<=y&&y<uy);
}

void dfs(vector<vector<int>> &s,int w,int h,int x,int y){
	s[y][x]=0;
	repi(dx,-1,2){
		repi(dy,-1,2){
			if(valid(0,w,0,h,x+dx,y+dy)&&s[y+dy][x+dx]==1){
				dfs(s,w,h,x+dx,y+dy);
			}
		}
	}
}

int main()
{
	int w,h;
	while(cin>>w>>h,w!=0&&h!=0){
		vector<vector<int>> s(h,vector<int>(w,0));
		rep(y,h)rep(x,w)cin>>s[y][x];

		int ans=0;
		rep(y,h){
			rep(x,w){
				if(s[y][x]==1)ans++,dfs(s,w,h,x,y);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<bitset>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<numeric>
using namespace std;

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

#define rep(i,x) for(int i=0;i<x;i++)
#define re(i,x,y) for(int i=x;i<y;i++)

long long INF=1e9;
const long long mod=2019;
#define ll long long

int H,W;
vector<vector<int>>field;
void dfs(int h,int w){
  field[h][w]=0;
  for(int dh=-1;dh<=1;dh++){
    for(int dw=-1;dw<=1;dw++){
      int nh=h+dh,nw=w+dw;
      if(nh<0||nh>=H||nw<0||nw>=W)continue;
      if(field[nh][nw]==0)continue;
      dfs(nh,nw);
    }
  }
}

int main(){
  while(cin>>W>>H){
    if(H==0||W==0)break;
    field.assign(H,vector<int>(W,0));
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        cin>>field[h][w];
      }
    }
    int count=0;
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        if(field[h][w]==0)continue;
        dfs(h,w);
        count++;
      }
    }
    cout<<count<<endl;
  }
}

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[i/99][i%99]=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();
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)

using namespace std;

    int w,h;
    int c[55][55];
    vector<vector<bool> > seen;
    int mx[8] = {-1,1,0,-1,1,-1,0,1};
    int my[8] = {0,0,1,1,1,-1,-1,-1};
    
    void dfs(int x,int y){
    	::seen[x][y] = true;
    	
    	for(int i = 0; i < 8; i++){
			int nx = x + mx[i];
			int ny = y + my[i];
			
			if(nx < 0 || nx >= h || ny < 0 || ny >= w)	continue;
			if(::seen[nx][ny])	continue;
			if(c[nx][ny] == 0)	continue;
			
			dfs(nx,ny);
		}
    }
int main(){
	while(true){
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	::seen.assign(55,vector<bool>(55,false)); //initilaization
	
	
	int count = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> c[i][j];
		}
	}
	
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(::seen[i][j])	continue;
			if(c[i][j] == 0)	continue;
			
			dfs(i,j);
			count++;
		}
	}
	cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i = 0 ; i < n ; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main()
{
	debug_io
	int w, h;
	while (cin >> w >> h, w + h) {
		int tile[55][55];
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

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

		int ans = 0;
		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] == 1) {
					vector<pair<int, int>> bfs;
					bfs.push_back({ i,j });

					while (bfs.size()) {
						auto now = bfs.front();
						bfs.erase(bfs.begin());
						int x = now.second;
						int y = now.first;
						tile[y][x] = 2;
						REP(k, 8) {
							int nx = max(0,min(x + dx[k],w-1));
							int ny = max(0, min(y + dy[k], h - 1));
							if (tile[ny][nx] == 1) {
								bfs.push_back({ ny,nx });
							}
						}
					}
					ans++;
					
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int w, h;
int c[51][51];
bool f[51][51];
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
bool contain(int y, int x){
   return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    rep(i, 0, 8){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(!contain(ny, nx)) continue;
        if(f[ny][nx] || c[ny][nx] == 0) continue;
        dfs(ny, nx);
    }
}

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