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>
using namespace std;

int field[50+2][50+2];
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};

int rec(int x, int y)
{
  int i;

  if(field[x][y]!=1) return 0;
  field[x][y] = 0;
  for(i=0;i<8;i++) rec(x+dx[i],y+dy[i]);
  return 1;
}

int main()
{
  int w,h;
  int i,j;
  int n;
  while(cin>>w>>h,w|h){
    for(j=1;j<=h;j++)
      for(i=1;i<=w;i++)
	cin>>field[i][j];
    for(j=1;j<=h+1;j++)
      field[w+1][j]=0;
    for(i=1;i<=w+1;i++)
      field[i][h+1]=0;

    n = 0;
    for(j=1;j<=h;j++) for(i=1;i<=w;i++) n += rec(i,j);

    cout<<n<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
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;}

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

int a[55][55],H,W;

void dfs(int i,int j,vector<vector<int>>&used){
	used[i][j] = true;
	rep(k,0,8){
		int ni = i + dy[k],nj = j + dx[k];
		if(ni<0||nj<0||H<=ni||W<=nj)continue;
		if(a[ni][nj]==0)continue;
		if(used[ni][nj])continue;
		dfs(ni,nj,used);
	}
}

signed main()
{
	//IOS();
	vector<int>ans;
	while(1){
		cin>>W>>H;
		if(W==0&&H==0)break;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				cin>>a[i][j];
			}
		}
		vector<vector<int>>used(H,vector<int>(W,0));
		int cnt = 0;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(used[i][j])continue;
				if(a[i][j]==0)continue;
				dfs(i,j,used);
				cnt++;
			}
		}
		ans.push_back(cnt);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

C++: 
/*
Problem

Category
DFS

URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

考察:


Date: 2020/04/14
*/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int w, h;
vector<vector<bool>> C;
vector<vector<int>> color;
int currentColor = 1;


/* ======================== */

signed main(){
    while(true){
        getInput();
        solve();
    }
    return 0;
}

void getInput() {
    cin>>w>>h;
    if(!w and !h) {exit(0);}
    C = vector<vector<bool>>(h, vector<bool>(w, false));
    color = vector<vector<int>>(h, vector<int>(w, 0));
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            bool c;
            cin>>c;
            C[i][j] = c;
        }
    }
}

void dfs(int i, int j) {
    if (i < 0 or j < 0 or i >= h or j >= w) return;
    if (!C[i][j]) return;
    if (color[i][j]) return;
    color[i][j] = currentColor;
    
    for(int a: {i-1, i, i+1})
        for(int b: {j-1, j, j+1})
            dfs(a, b);
    
}


void solve() {
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            dfs(i, j);
            currentColor++;
        }
    }
    set<int> colorSet {0};
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            colorSet.insert(color[i][j]);
        }
    }
    cout<<colorSet.size()-1<<endl;
}

C++: #include <iostream>

using namespace std;

// 0: 海, 1: 陸
int board[50][50] = {{0}};

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

bool DFS(int i, int j, int w, int h) {
  if (i < 0 || i >= h || j < 0 || j >= w) return false;
  if (!board[i][j]) return false;

  board[i][j] = 0;
  for (int d = 0; d < 8; ++d)
    DFS(i + dy[d], j + dx[d], w, h);

  return true;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) break;

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

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (DFS(i, j, w, h)) ++ans;
      }
    }
    cout << ans << endl;
  }
  return 0;
}