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

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T    In(T& x){ cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Solver {
  Solver(int w, int h) : w(w), h(h) { memset(field, 0, sizeof(field)); }
  void input(){
    for (int y = 0; y < h; y++){
      for (int x = 0; x < w; x++){
        field[y][x] = in();
      }
    }
  }
  int solve(){
    int count = 0;
    for (int y = 0; y < h; y++){
      for (int x = 0; x < w; x++){
        if (field[y][x]){
          count++;
          erase(x, y);
        }
      }
    }
    return count;
  }

  void erase(int x, int y){
    if (invalid(x, y)) return;
    if (!field[y][x]) return;
    field[y][x] = 0;
    for (int d = 0; d < 8; d++){
      erase(x + dx[d], y + dy[d]);
    }
  }
  bool invalid(int x, int y){
    return x < 0 || w <= x || y < 0 || h <= y;
  }
  const int w, h;
  int field[64][64];
};

int main()
{
  int w, h;
  while (w = in(), h = in()){
    Solver s(w, h);
    s.input();
    printf("%d\n", s.solve());
  }
  return (0);
}

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

int w,h;
int maps[50][50];

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1 && xx >= 0 && xx < w && yy >= 0 && yy < h) dfs(yy,xx);
    }
  }
}

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

      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  if(maps[i][j] == 1){
	    island++;
	    dfs(i,j);
	  }
	}
      }
      cout << island << endl;
    }
  return 0;
}

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

static const int MAX_H = 50;
static const int MAX_W = 50;

int H, W;
char grid[MAX_H][MAX_W + 1];
bool reached[MAX_H][MAX_W];

#define OCTAS
#ifdef OCTAS
static const int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
static const int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};
#else
static const int dh[]{-1, 0, 0, +1};
static const int dw[]{0, -1, +1, 0};
#endif

bool dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < sizeof(dh) / sizeof(int); ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == '0' || reached[nh][nw])
      continue;
    if (dfs(nh, nw))
      return true;
  }

  return false;
}

int main() {
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        cin >> grid[h][w];
      }
    }
    fill_n(reached[0], MAX_H * MAX_W, false);
    int count{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == '0' || reached[h][w])
          continue;
        dfs(h, w);
        ++count;
      }
    }
    cout << count << endl;
  }
}