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;
const int MAXW = 51;
const int MAXH = 51;
int map[MAXW][MAXH];
int w,h;
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};
int dfs(int x,int y){
  if(map[x][y] == 0)return 0;
  map[x][y] = 0;
  for(int i = 0;i < 9;i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx >= 0 && nx < w & ny >= 0 && ny < h)
      dfs(nx,ny);
  }
  return 1;
}

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

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

typedef long long ll;
using namespace std;

int H, W;
vector<vector<int>> C;
vector<vector<int>> visited;
int ans = 0;





void dfs(int &i, int &j){
    visited[i][j] = 1;
    for(int y=-1; y<=1; y++){
        for(int x=-1; x<=1; x++){
            int ny = i+y;
            int nx = j+x;
            
            if(ny>=0 && ny<H && nx>=0 && nx<W && C[ny][nx] && !visited[ny][nx]) dfs(ny, nx);
        }
    }
    return;
}




int main() {
    while(1){
        ans = 0;
        visited.clear();
        
        cin >> W >> H; if((W&H)==0) break;
        C.resize(H);
        for(vector<int> &v:C) v.resize(W);
        for(vector<int> &row:C)
            for(int &c:row) cin >> c;

        visited.resize(H);
        for(vector<int> &v:visited) v.resize(W);

        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(C[i][j] && !visited[i][j]){
                    dfs(i,j);
                    ans ++;
                }
            }
        }
        
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
//using ll=long long;
const int MX = 2200000;
//const ll LINF = 1000000000000000000ll;
//const long long MOD = 1000000007;
//long long INF = 90000000001000;
int w,h;
bool seen[50][50];
int c[50][50];
int d[2]={1,-1};
void dfs(int i,int j){
    seen[i][j]=true;
    for (int k = 0; k < 2; ++k)
    {
       if (!seen[i+d[k]][j]&&i+d[k]<h&&i+d[k]>=0&&c[i+d[k]][j])dfs(i+d[k],j);
       if (!seen[i][j+d[k]]&&j+d[k]<w&&j+d[k]>=0&&c[i][j+d[k]])dfs(i,j+d[k]);
       if (!seen[i+d[k]][j-d[k]]&&i+d[k]<h&&i+d[k]>=0&&j-d[k]>=0&&j-d[k]<w&&c[i+d[k]][j-d[k]])dfs(i+d[k],j-d[k]);
       if (!seen[i+d[k]][j+d[k]]&&i+d[k]<h&&i+d[k]>=0&&j+d[k]<w&&j+d[k]>=0&&c[i+d[k]][j+d[k]])dfs(i+d[k],j+d[k]); 
    }

}


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 >> c[i][j];
       seen[i][j]=false;
   }
}
int cnt=0;
for (int i = 0; i < h; ++i)
{
  for (int j = 0; j < w; ++j)
  {
    if(c[i][j]==1&&!seen[i][j]){cnt++;dfs(i,j);}
  }
}
cout << cnt << endl;
}

return 0;

}

C++: #include<cstdio>
#include<stack>

using namespace std;

struct Coord {
  int row;
  int col;
};

int solve (int W, int H);
  
int main () {
  int W, H;

  while(1)
    {
      scanf("%d%d", &W, &H);
      if(W==0 && H==0) break;	
      printf("%d\n", solve(W,H));	
    }
}

int solve (int W, int H) {
  int n=0;
  char M[H+2][W+2];
  Coord start;
  for (int i=0; i<=W+1; i++) { M[0][i] = '0'; M[H+1][i] = '0'; }
  for (int i=1; i<=H; i++) { M[i][0] = '0'; M[i][W+1] = '0'; } 
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      scanf(" %c", &M[i][j]);
    }
  }
  for(int i=1 ; i<=H ; i++) {
    for(int j=1 ; j<=W ; j++){
      if(M[i][j]=='1'){	
        start.row=i;
        start.col=j;
        M[start.row][start.col]='@';

        stack<Coord> S;
        S.push(start);
        while (!S.empty()) {
          Coord current = S.top(); S.pop();
          int D1[]={1,1,0,-1,-1,-1,0,1}; int D2[]={0,-1,-1,-1,0,1,1,1};
          for (int d=0; d<8; d++) {
            Coord nc = current;
            nc.row += D1[d];
            nc.col += D2[d];
            if(M[nc.row][nc.col]!='0' && M[nc.row][nc.col]!='*' && M[nc.row][nc.col]!='@')
	      {
	        M[nc.row][nc.col] = '*';
	        S.push(nc);
	      }
          } 
        }
        n++;
      }
    }
  }
  return n;
}

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

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

int main(){
    int w, h;
    int field[50][50];
    int result;
    cin >> w >> h;
    while(w || h){
        result = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                cin >> field[j][i];
            }
        }
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                result += fill(field, j, i, w, h);
            }
        }
        cout << result << endl;
        cin >> w >> h;
    }
}