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>
#define int long long
#define double long double
#define INF 1e18
using namespace std;

int W, H;
int c[1000][1000];

void dfs(int x, int y) {
  int moveX[3] = {0,-1,1};
  int moveY[3] = {0,-1,1};
  c[x][y] = 0;
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      int nx=x+moveX[i], ny=y+moveY[j];
      if(nx<0||ny<0) continue;
      if(c[nx][ny] == 1) dfs(nx,ny);
    }
  }
}

signed main() {
  cin>>W>>H;
  while (W+H!=0) {
    // i = y, j = x
    for(int i=0;i<1000;i++)for(int j=0;j<1000;j++)c[i][j]=0;
    for(int i=0;i<H;i++){
      for(int j= 0;j<W;j++){
        cin>>c[i][j];
      }
    }
    
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (c[i][j] == 1) {
          dfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
    cin >> W >> H;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <array>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cstdlib>
#include <ctime>
#include <cstdint>
#include <cmath>

#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)
#define EACH(i,x)   for(auto & i : x)
#define ALL(x)      begin(x),end(x)

using namespace std;

typedef vector<int> vecint;

int root(int i,vecint& p){
  if(p[i] != i+1)
    p[i] = root(p[i]-1,p);
  return p[i];
}
void join(int i,int j,vecint& p){
  p[root(i,p)-1] = root(j,p);
}

int main()
{
  while(true){
    int w,h;cin>>w>>h;
    if(!(w||h))break;
    vector<vecint> m(h+2,vecint(w+2));
    REP(i,h)REP(j,w)cin>>m[i+1][j+1];
    int n=(w+2)*(h+2);
    vecint p(n);
    REP(i,n)p[i]=i+1;
    REP(i,h)REP(j,w){
      int di[8] = {1,1,1,0,-1,-1,-1,0};
      int dj[8] = {1,0,-1,-1,-1,0,1,1};
      REP(k,8){
        int ni=i+di[k];
        int nj=j+dj[k];
        if (m[i+1][j+1] && m[ni+1][nj+1]){
          int ijn = (i+1)*(w+2)+(j+1);
          int nijn = (ni+1)*(w+2)+(nj+1);
          join(ijn,nijn,p);
        }
      }
    }
    set<int> s;
    REP(i,h)REP(j,w){
      if(m[i+1][j+1]){
        int ijn = (i+1)*(w+2)+(j+1);
        s.insert(root(ijn,p));
      }
    }
    cout<<s.size()<<endl;
  }
  return 0;
}

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

#define H_MAX 50
#define W_MAX 50

int field[H_MAX][W_MAX];

int w, h;

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

void dfs(int y, int x) {
    field[y][x] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx]) {
            dfs(ny, nx);
        }
    }
}

int count() {
    int c = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j]) {
                c++;
                dfs(i, j);
            }
        }
    }
    return c;
}

int main() {
    memset(field, 0, sizeof(field));
    while (cin >> w >> h) {
        if (w == 0 && h == 0) break;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> field[i][j];
            }
        }
        cout << count() << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

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

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

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

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
    // for (int y = 0; y < h; y++) {
    //     for (int x = 0; x < w; x++)
    //         cout << m[y][x];
    //     cout << endl;
    // }
    // cout << endl;

                    dfs(i, j);
                    isl++;
                }
            
        cout << isl << endl;
    }

    return 0;
}

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

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

void dfs(int y, int x){
	maze[y][x] = 0;
	//search8deg
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			int nx = x - j;
			int ny = y - i;
			if(0 <= nx and nx < w and 0 <= ny and ny < h and maze[ny][nx] == 1){
				dfs(ny, nx);
			}
		}
	}
}

int main(){
while(1){	
	
	
	cin >> w >> h;
	if(w == 0 and h == 0) break;
	
	for(int i=0; i < h; i++){
		for(int j=0; j < w; j++){
			cin >> maze[i][j]; 
		}
	}
	
	int ans = 0;
	for(int i=0; i < h; i++){
		for(int j=0; j < w; j++){
			if(maze[i][j] == 1){
				dfs(i, j);
				ans += 1;
			}
		}
	}
	
	cout << ans << endl;
	
	
}//END
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}
}
int main(){
	a=0;
	s(n);s(m);
	if(n==0)return 0;
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}