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>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      
      for(i=0;i<w;i++){
	if(p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  p[i][j]=0;
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    if(n.x+1<w&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      p[n.x+1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y+1<h&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      p[n.x][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      p[n.x-1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y-1>=0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      p[n.x][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      p[n.x+1][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      p[n.x-1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      p[n.x+1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      p[n.x-1][n.y+1]=0;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x,y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cout << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) (find(all(v),x)!=v.end())
#define incls(s,c) (s.find(c)!=string::npos)
#define lb(a,x) distance((a).begin(),lower_bound(all(a),(x)))
#define ub(a,x) distance((a).begin(),upper_bound(all(a),(x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)),x.end())
#define SP << " " <<
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
//const long double EPS = 1e-14;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
inline int in() { int x; cin >> x; return x;}
inline ll lin() { ll x; cin >> x; return x;}
inline char chin() { char x; cin >> x; return x;}
inline string stin() { string x; cin >> x; return x;}
inline double din() { double x; cin >> x; return x;}
//template<class T = int> inline T in() { T x; cin >> x; return (x);}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
char itoa(int n) { return n + '0';}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const int dx[8] = {1, 1, 0, -1, -1, -1,  0,  1};
const int dy[8] = {0, 1, 1,  1,  0, -1, -1, -1};

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

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

int main () {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    rep(i,h)rep(j,w) cin >> c[i][j];
    int ans = 0;
    rep(i,h)rep(j,w) {
      if (c[i][j]) {
        ans++;
        dfs(i,j);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void b(int ,int );
int a[100][100]={};
 
int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
     
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    cin>>a[i][j];
 
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    if(a[i][j]==1){
      dfs(j,i);
      count++;
    }
    cout<<count<<endl;  
  }
  return 0;
}
 
  void b(int x,int y){
    a[y][x]=0;
    int x1,y1;
    int x2[8]={-1,0,1,1,1,0,-1,-1};
    int y2[8]={1,1,1,0,-1,-1,-1,0};
    for(int k=0;k<8;k++){
    x1=x+x2[k];
    y1=y+y2[k];
    if(a[y1][x1]==1)
      dfs(x1,y1);
    }
  }