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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline long toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline long toLong(string s) {long v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const int INF = 10000;

char mp[52][52];

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

bool bfs(int s,int t){
	if(flag[s][t])return false;
	int sum=0;
	queue<PII>pi;
	pi.push(MP(s,t));
	flag[s][t]=true;
	while(!pi.empty()){
		PII p=pi.front();pi.pop();
		sum++;
		REP(i,8){
			int y=p.first+dy[i],x=p.second+dx[i];
			if(mp[y][x]=='1'&&!flag[y][x]){
				flag[y][x]=true;
				pi.push(MP(y,x));
			}
		}
	}
	return true;
}
int main(){
	int W,H;
	while(cin>>W>>H){
		if(W==0&&H==0)break;
		REP(i,52)REP(j,52){
			mp[i][j]='0';
		}
		CLR(flag);
		int y,x;
		REP(i,H)REP(j,W){
			cin>>mp[i+1][j+1];
		}
		int sum=0;
		REP(i,H)REP(j,W){
			if(mp[i+1][j+1]=='1'&&bfs(i+1,j+1))sum++;
		}
		//cout<<"!"<<y<<" "<<x<<endl;
		cout<<sum<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int mp[51][51];
void init(){
  for(int i = 0; 51 > i; i++){
    for(int j = 0; 51 > j; j++){
      mp[i][j] == 0;
    }
  }
}
int tansa(int h,int w, int nh, int nw){
  if(mp[nh][nw] == 1)mp[nh][nw] = 0;
  if(nh != 0 && nw != 0 && mp[nh-1][nw-1] == 1)tansa(h,w,nh-1,nw-1);
  if(           nw != 0 && mp[nh  ][nw-1] == 1)tansa(h,w,nh  ,nw-1);
  if(nh!=h-1 && nw != 0 && mp[nh+1][nw-1] == 1)tansa(h,w,nh+1,nw-1);
  if(nh != 0 &&            mp[nh-1][nw  ] == 1)tansa(h,w,nh-1,nw  );
  if(nh!=h-1 &&            mp[nh+1][nw  ] == 1)tansa(h,w,nh+1,nw  );
  if(nh != 0 && nw!=w-1 && mp[nh-1][nw+1] == 1)tansa(h,w,nh-1,nw+1);
  if(           nw!=w-1 && mp[nh  ][nw+1] == 1)tansa(h,w,nh  ,nw+1);
  if(nh!=h-1 && nw!=w-1 && mp[nh+1][nw+1] == 1)tansa(h,w,nh+1,nw+1);
  return 0;
}
int main(){
  while(true){
    int w,h;cin>>w>>h;
    if(w == 0)break;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        cin >> mp[i][j];
      }
    }
    int ret = 0;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        if(mp[i][j] == 1){
          tansa(h,w,i,j);
          ret++;
        }
      }
    }
    cout << ret << endl;
    init();
  }
}