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;
using ll=long long;
#define fi first
#define se second
using Graph=vector<vector<int>>;
using P=pair<int,int>;
#define mp make_pair

void dfs(int u,int v,Graph graph,Graph &d,int flag)
{
	int h,w;
	h=graph.size();
	w=graph[0].size();
	int n=graph.size();
	d[u][v]=flag;
	vector<P> around={mp(u-1,v),mp(u+1,v),mp(u-1,v-1),mp(u-1,v+1),mp(u,v+1),mp(u,v-1),mp(u+1,v+1),mp(u+1,v-1)};
	for (int i=0;i<8;i++)
	{
		int x=around[i].fi,y=around[i].se;
		if (0<=x&&x<h && 0<=y&&y<w)
		{
			if (graph[x][y]==1 && d[x][y]==-1)
			{
				//cout << x << y << endl;
				dfs(x,y,graph,d,flag);
			}

		}
	}
}

int main()
{
	vector<int> ans;
	while (true)
	{
		int w,h;cin >> h >> w;
		if (w==0 && h==0) break;
		Graph graph(w,vector<int> (h,0)),d(w,vector<int> (h,0));
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				cin >> graph[i][j];
				if (graph[i][j]==1) d[i][j]=-1;
			}
		}
		int flag=1;
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				if (d[i][j]==-1)
				{
					dfs(i,j,graph,d,flag);
					flag++;
				}
			}
		}
		ans.push_back(flag-1);
	}
	for (int i=0;i<ans.size();i++) cout << ans[i] << 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],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]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}}int main(){a=0;s(n)s(m)if(n==0)return 0;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();}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int W,H;

int t[50][50];

void visit(int h,int w){
  t[h][w]=0;
  for(int nh=h-1;nh<h+2;nh++){
    for(int nw=w-1;nw<w+2;nw++){
      if(t[nh][nw]==1&&0<=nh&&0<=nw&&nh<H&&nw<W)visit(nh,nw);
    }
  }
}

int main(void){
  int a,b,c,n;
  while(1){
    cin>>W>>H;
    c=0;
    if(W==0&&H==0)break;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    cin>>t[i][j];
      }
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    if(t[i][j]==1){
      c++;
      visit(i,j);
    }
      }
    }
    cout<<c<<endl;
  }
}

C++: #include <iostream>
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

int w,h;
int c[51][51];

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

void dfs(int x,int y) {
    c[y][x] = 0;
    
    rep(i,8) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1) dfs(nx,ny);
    }
}
    
int main(void){
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(y,h) rep(x,w) cin >> c[y][x];
        
        int ans = 0;        
        rep(y,h)
            rep(x,w)
                if (c[y][x] == 1) {
                    dfs(x,y);
                    ans++;
                }

        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
bool find1(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  for(int i = 0;i < C.size();++i)
    for(int j = 0;j < C[0].size();++j)
      if(C[i][j] == 1 && check[i][j] == 0){
	*findh = i;
	*findw = j;
	return true;
      }
  return false;
}
bool find2(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  if(C[*findh - 1][*findw - 1] == 1 && check[*findh - 1][*findw - 1] == 0){
    *findh -= 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw] == 1 && check[*findh - 1][*findw] == 0){
    *findh -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw + 1] == 1  && check[*findh - 1][*findw + 1] == 0){
    *findh -= 1;
    *findw += 1;
    return true;
  }
  else if(C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0){
    *findw -= 1;
    return true;
  }
  else if(C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0){
    *findw += 1;
    return true;
  }
  else if(C[*findh + 1][*findw - 1] == 1 && check[*findh + 1][*findw - 1] == 0){
    *findh += 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0){
    *findh += 1;
    return true;
  }
  else if(C[*findh + 1][*findw + 1] == 1 && check[*findh + 1][*findw + 1] == 0){
    *findh += 1;
    *findw += 1;
    return true;
  }
  else return false;
}
int main()
{
  int w,h,findh,findw;
  stack< pair<int,int> > finish;
  queue<int> answer;
  while(cin >> w >> h && !(w == 0 && h == 0)){
    int howmany = 0;
    vector< vector<int> > C(h + 2,vector<int>(w + 2,0)),check(h + 2,vector<int>(w + 2,0)); 
    for(int i = 1;i <= h;++i)
      for(int j = 1;j <= w;++j)
	cin >> C[i][j];
    while(find1(C,check,&findh,&findw)){
      check[findh][findw] = 1;
      finish.push(make_pair(findh,findw));
      while(!finish.empty()){
	findh = finish.top().first;
	findw = finish.top().second;
	while(find2(C,check,&findh,&findw)){
	  finish.push(make_pair(findh,findw));
	  check[findh][findw] = 1;
	}
	finish.pop();
      }
      ++howmany;
    }
    answer.push(howmany);
  }
  for(;!answer.empty();answer.pop())
    cout << answer.front() << endl;
}