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

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

void dfs(int x, int y){
	map[y][x] = 0;
	for(int i = -1; i <= 1; i++){
		for(int j = -1; j <= 1; j++){
			int nx = x + i, ny = y + j;
			if(nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] == 1)	dfs(nx, ny);
		}
	}
	return;
}

int main(void){
	int cnt;
	while(true){
		cin >> w >> h;
		if(!w && !h)	break;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> map[i][j];
			}
		}
		cnt = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(map[i][j] == 1){
					dfs(j, i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
  return 0;
}

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

int w,h;
int mp[50][50];
void dfs(int y,int x){

  mp[y][x] = 0;
  for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++){
      int ny = y + i;
      int nx = x + j;
      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(mp[ny][nx] == 0) continue;
      dfs(ny,nx);
    }
}

signed main(){
  while(1){
    cin>>w>>h;
    if( w == 0 && h == 0) return 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++) cin>>mp[i][j];

    int ans = 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
        if(mp[i][j] == 0) continue;
        ans++;
        dfs(i, j);
      }

    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
 
int w, h;
int c[50][50];
int dx[8]={-1,0,1,-1,1,-1,0,1,};
int dy[8]={-1,-1,-1,0,0,1,1,1};
 
void dfs(int x,int y){
  c[y][x]=0;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(ny>=0&&ny<h&&nx>=0&&nx<w){
      if(c[ny][nx]==1) dfs(nx, ny);
    }
  }
}
 
int main(){
 
  int ans;
 
  while(cin>>w>>h, w){
 
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
     
    ans=0;
     
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]==0) continue;
	if(c[i][j]==1){
	  ans++;
	  dfs(j, i);
	}
      }
    }
     
    cout<<ans<<endl;
  }
     
     
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

bool island[50][50];
bool visited[50][50];

void visit(int x, int y) {
  visited[y][x] = true;
  if (x > 0 && island[y][x-1] && !visited[y][x-1]) visit(x-1, y);
  if (x < 49 && island[y][x+1] && !visited[y][x+1]) visit(x+1, y);
  if (y > 0 && island[y-1][x] && !visited[y-1][x]) visit(x, y-1);
  if (y < 49 && island[y+1][x] && !visited[y+1][x]) visit(x, y+1);
  if (x > 0 && y > 0 && island[y-1][x-1] && !visited[y-1][x-1]) visit(x-1, y-1);
  if (x > 0 && y < 49 && island[y+1][x-1] && !visited[y+1][x-1]) visit(x-1, y+1);
  if (x < 49 && y > 0 && island[y-1][x+1] && !visited[y-1][x+1]) visit(x+1, y-1);
  if (x < 49 && y < 49 && island[y+1][x+1] && !visited[y+1][x+1]) visit(x+1, y+1);
}

int main() {
  int w, h, tmp, cnt;
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        island[i][j] = false;
        visited[i][j] = false;
      }
    }
    cnt = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (cin >> tmp, tmp)
          island[i][j] = true;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (island[i][j] && !visited[i][j]) {
          visit(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << el;
  }
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stacK>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

vector<int> dx{1, 0, -1, 0, 1, -1, 1, -1};
vector<int> dy{0, 1, 0, -1, -1, 1, 1, -1};

vector<vector<int>> g;
int w, h;

vector<vector<bool>> seen;
void dfs(int x, int y){
  seen[y][x] = true;
  for(int i = 0; i < 8; ++i){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny)continue;
    else if(!g[ny][nx])continue;
    else if(seen[ny][nx])continue;
    dfs(nx, ny);
  }
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)return 0;
    g = vector<vector<int>>(h, vector<int>(w));
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        cin >> g[i][j];
      }
    }

    seen = vector<vector<bool>>(h, vector<bool>(w, false));
    int cnt = 0;
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        if(seen[i][j])continue;
        if(!g[i][j])continue;
        dfs(j, i);
        ++cnt;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}