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 <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int H, W, m[55][55], dx[] = {1,1,0,-1,-1,-1,0,1}, dy[] = {0,1,1,1,0,-1,-1,-1};
bool flag[55][55];

void dfs(int y, int x) {
	for (int k = 0; k < 8; k++) {
		int nx = x + dx[k] , ny = y + dy[k];
		if (0<=ny && ny<H && 0<=nx && nx<W) {
			if (flag[ny][nx] && m[ny][nx]==1) {
				flag[ny][nx] = false;
				dfs(ny, nx);
			}
		}
	}
}

int main() {
	
	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 >> m[i][j];
				flag[i][j] = true;
			}
		}

		int cnt = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (m[i][j] == 1 && flag[i][j]) cnt++;
				flag[i][j] = false;
				if(m[i][j]==1) dfs(i, j);
			}
		}

		cout << cnt<<endl;

	}
	return 0;
}

C++: #include <stdio.h>
int i,j,RB[51][51],N,M,ans,a[8][2]={-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1};void bfs(int x, int y) {RB[x][y]=0;for (int q=0,nx,ny;q<8;q++) {nx=x+a[q][0],ny=y+a[q][1];if (nx>=0 && nx<N && ny>=0 && ny<M && RB[nx][ny]) bfs(nx,ny);}}
int main() {while (1) {scanf("%d %d",&M,&N);if (!N && !M) return 0;ans=0;for (i=0;i<N;i++) for (j=0;j<M;scanf("%d",&RB[i][j++]));for (i=0;i<N;i++) for (j=0;j<M;j++) if (RB[i][j]) bfs(i,j),ans++;printf("%d\n",ans);}}

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

int visited[50][50];
const int offset[8][2] = {
    {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}
};
int grid[50][50];

void dfs(int i, int j, int h, int w){
    for (int k = 0; k < 8; k++) {
        int ii = i + offset[k][0], jj = j + offset[k][1];
        if (ii < 0 || ii >= h || jj < 0 || jj >= w) {
            continue;
        }
        if (!visited[ii][jj] && grid[ii][jj]) {
            visited[ii][jj] = 1;
            dfs(ii, jj, h, w);
        }
    }
}


int main(int argc, char *argv[])
{
    
    cin.tie(0);
    ios::sync_with_stdio(false);

    int w, h;

    
    while (cin >> w >> h, w != 0 && h != 0) {
        memset(visited, 0, 50*50*sizeof(int));
        memset(grid, -1, 50*50*sizeof(int));
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> grid[i][j];
            }
        }
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (visited[i][j] || !grid[i][j]) {
                    continue;
                }
                dfs(i, j, h, w);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    
    return 0;
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int dx[8]={1,1,1 ,0, 0,-1,-1,-1};
int dy[8]={0,1,-1,1,-1, 1, 0,-1};
int c[55][55];
int w,h;
int res;
void paint(int i,int j,int k){
  if(i<0||j<0)return;
  else if(i>=h||j>=w) return;
  else if(c[i][j]!=1) return;
  c[i][j]=k;
  for(int l=0;l<8;l++) paint(i+dx[l],j+dy[l],k);
  return;
}
void count(int i,int j){
  res++;
  paint(i,j,res);
  return;
}
int main(void){
  int i,j;
  while(scanf("%d %d",&w,&h),w|h){
    res=1;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]==1){
          count(i,j);
        }
      }
    }
    printf("---\n");
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        printf("%d",c[i][j]);
      }
      printf("\n");
    }
    printf(":%d\n",res-1);
  }
  return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int w, h;
bool F[60][60];

struct point {
  int x, y;
};

void init() {
  fill(&F[0][0], &F[0][0]+60*60, 0);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      int temp;
      scanf("%d", &temp);
      F[i][j] = (bool) temp;
    }
  }
}

int solve() {
  int ans = 0;
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      if (F[i][j]) {
	stack<point> S;
	point p = { i, j };
	S.push(p);
	while (! S.empty()) {
	  p = S.top();
	  S.pop();
	  F[p.x][p.y] = false;
	  for (int i=-1; i<=1; i++) {
	    for (int j=-1; j<=1; j++) {
	      point q = { p.x + i , p.y + j };
	      if (F[q.x][q.y]) {
		S.push(q);
	      }
	    }
	  }
	}
	ans++;
      }
    }
  }
  return ans;
}

int main() {
  while(scanf("%d%d", &w, &h) != EOF && w && h) {
    init();
    printf("%d\n", solve());
  }
}