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;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int h, w;
vector<vector<bool>> c;
vector<vector<bool>> v;
const int dx[8] = { 0, 1, 1,  1,  0, -1, -1, -1 };
const int dy[8] = { 1, 1, 0, -1, -1, -1,  0,  1 };

bool dfs(const int x, const int y) {
    // cout << x << " " << y << " " << c[x][y] << " " << v[x][y] << endl;
    if(not c[x][y]) return false; // sea
    if(v[x][y]) return false; // already visited
    v[x][y] = true;

    for (auto i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 or nx >= h or ny < 0 or ny >= w) continue;
        dfs(x + dx[i], y + dy[i]);
    }

    return true;
}

int main(){
    while(true) {
        cin >> w >> h;
        if(w == 0 and h == 0) break;

        c.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                int val; cin >> val;
                c[i][j] = static_cast<bool>(val); // true : land, false : sea
            }
        }

        int ans = 0;
        v.assign(h, vector<bool>(w, false));
        for (auto i = 0; i < h; i++) {
            for (auto j = 0; j < w; j++) {
                if(dfs(i, j)) ans++;
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

int w, h, cnt;
int island[51][51];
int dx[] = {1, 1, 0, -1, -1, -1,  0,  1};
int dy[] = {0, 1, 1,  1,  0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || ny < 0 || w <= nx || h <= ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

  while(cin >> w >> h, w || h){

    for(int i = 0 ; i < h ; i++)
      for(int j = 0 ; j < w ; j++)
	cin >> island[i][j];

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

C++: #include <iostream>
using namespace std;

int islands=0;
int w,h;
int field[50][50];
void bfs(int, int);

int main(){

  while(cin>>w>>h&&w!=0&&h!=0){

    for(int i=0;i<50;i++){
      for(int j=0;j<50;j++){
        field[i][j] = -1;
      }
    }

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        cin >> field[i][j];
        //        cout << field[i][j] << " ";
      }
      //    cout << endl;
    }
    // cout << endl;
    int ans=0; 

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        if(field[i][j]==1){
          field[i][j]=0;
          bfs(i,j);
          ans++;
        }
      }
    }
    /*   if(w==5&&h==4){
         for(int i=0;i<w;i++){
         for(int j=0;j<h;j++){
         cout << field[i][j] << " ";
         }
         cout << endl;
         }
         }*/
    cout << ans << endl;
  }

  return 0;
}

void bfs(int x, int y){
  if(x-1>=0&&field[x-1][y]==1){
    field[x-1][y]=0;
    bfs(x-1,y);
  }
  if(x+1<w&&field[x+1][y]==1){
    field[x+1][y]=0;
    bfs(x+1,y);
  }
  if(y-1>=0&&field[x][y-1]==1){
    field[x][y-1]=0;
    bfs(x,y-1);
  }
  if(y+1<h&&field[x][y+1]==1){
    field[x][y+1]=0;
    bfs(x, y+1);
  }
  if(x-1>=0&&y-1>=0&&field[x-1][y-1]==1){
    field[x-1][y-1]=0;
    bfs(x-1,y-1);
  }
  if(x+1<w&&y-1>=0&&field[x+1][y-1]==1){
    field[x+1][y-1]=0;
    bfs(x+1,y-1);
  }
  if(x-1>=0&&y+1<h&&field[x-1][y+1]==1){
    field[x-1][y+1]=0;
    bfs(x-1,y+1);
  }
  if(x+1<w&&y+1<h&&field[x+1][y+1]==1){
    field[x+1][y+1]=0;
    bfs(x+1,y+1);
  }
  return;
}

C++: //
//AOJ vol11-1160
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
//20170616
//640009E
//Yang Bo
//

#include <iostream>

using namespace std;

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

int recursion(int y, int x)
{
  int number = 1;
  a[y][x] = 0;

  if (x < w - 1)
    if (a[y][x + 1] == 1)
      number = recursion(y, x + 1);
  if (y < h - 1)
    if (a[y + 1][x] == 1)
      number = recursion(y + 1, x);
  if (x > 0)
    if (a[y][x - 1] == 1)
      number = recursion(y, x - 1);
  if (y > 0)
    if (a[y - 1][x] == 1)
      number = recursion(y - 1, x);
  if (y < h - 1 || x < w - 1)
    if (a[y + 1][x + 1] == 1)
      number = recursion(y + 1, x + 1);
  if (y < h - 1 || x > 0)
    if (a[y + 1][x - 1] == 1)
      number = recursion(y + 1, x - 1);
  if (y > 0 || x < w - 1)
    if (a[y - 1][x + 1] == 1)
      number = recursion(y - 1, x + 1);
  if (y > 0 || x > 0)
    if (a[y - 1][x - 1] == 1)
      number = recursion(y - 1, x - 1);
  

  //cout << "recursion(" << y << ", "<< x << ") = " << number << endl;
  return number;
}

int main()
{
  while (cin >> w >> h) {
    if (w == 0)
      break;
    else {
      for (int i = 0; i < h; i++) {
	for (int j = 0; j < w; j++) {
	  cin >> a[i][j]; //??\???
	}
      }
      int ans = 0;
      for (int i = 0; i < h; i++) {
	for (int j = 0; j < w; j++) {
	  if (a[i][j] == 1) {
	    //cout << "i = " << i << " ,j = " << j << endl;
            ans += recursion(i, j);
	  }
	}
      }
      cout << ans << endl;
    }
  }
}

/*
??¢?????´??????????????????????????????????????????????´°?????????????°???\?????????????£???????????????£???????????£??????
*/