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>
using namespace std ;
int H , W ;
int board[550][550] ;

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

int main () {
  while(1){
    int ans = 0 ;
    cin >> W >> H ;
    if(H==0&&W==0) break ;
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        cin >> board[i][j] ;
      }
    }
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        if(board[i][j]==1){
          dfs(i,j) ;
          ans ++ ;
        }
      }
    }
    cout << ans << endl ;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int w,h;
int del(int x,int y){
  if(x<0)return 0;
  if(c[x][y]){
    c[x][y] = 0;
    return del(x+1,y)+del(x,y+1)+del(x+1,y+1)+
      del(x-1,y-1)+del(x-1,y)+del(x,y-1)+del(x+1,y-1)+del(x-1,y+1);
    
  }
  else return 0;
  
}
int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}
int main(){
  while(cin>>w>>h&&w&&h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    
    cout << solve(0,0) << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int Map[52][52];
bool used[52][52];
const int dir[][2] = { {-1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, {0, -1 }, {-1, -1 } };
int w, h;

void dfs (int row, int col )
{
	used[row][col] = true;
	rep (k, 8 ){
		int nr = row + dir[k][0];
		int nc = col + dir[k][1];
		if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 || used[nr][nc] ) continue;
		dfs (nr, nc );
	} // end rep
}

int main()
{
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (used, false, sizeof (used ) );
		rep (i, h ){
			rep (j, w ){
				scanf (" %d", Map[i]+j );
			} // end rep
		} // end rep
		int cnt = 0;
		rep (i, h ){
			rep (j, w ){
				if (Map[i][j] == 1 && !used[i][j] ){
					dfs (i, j );
					cnt++;
				} // end if
			} // end rep
		} // end rep
		printf ("%d\n", cnt );
	} // end loop
		
	return 0;
}

C++: /*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include<cstring>
//namespace
using namespace std;
//繰り返し
#define REP(i, m, n) for(int i=(int)m; i<(int)n; ++i)
#define rep(i, n) REP(i, 0, n)
//イテレータ
#define all_range(C) begin(C), end(C)
//簡略化
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
typedef pair<string,int> pst;

const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
//最大最小
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) {if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) {if(a<b) a=b;}

//関数

//最大公約数を求める
ll my_gcd(ll x, ll y) {
  ll r;

  while(y!=0) {
    r = x%y;
    x = y;
    y = r;
  }

  return x;
}

//最小公約数を求める
ll my_lcm(ll x, ll y) {

  return (x*y)/my_gcd(x,y);
}

//xのy乗を求める ※参考：10^18乗まで
ll my_pow(ll x, ll y) {
  ll sum = 1;

  while(y>0) {
    sum *= x;
  }

  return sum;
}

/*考えを書くスペース
xxxxxxxxxx  0000000000 最初のsearchで
xoooooooxx  0111111100
xxoooooxxx  0011111000
xxxoooxxxx  0001110000
xxxxoxxxxx  0000100000
xxxxxxxxxx  0000000000
xxxxoxxxxx  0000200000
xxxoooxxxx  0002220000
xxoooooxxx  0022222000
xxxxxxxxxx  0000000000
*/
//main.cpp----------------------------
#define max_h 50
#define max_w 50

void solve();
void search(int,int);

int h, w;
int c[max_h][max_w];

int main() {

  solve();

  return 0;
}

void solve() {

  while(1) {
    scanf("%d %d", &w, &h);
    if(w==0&&h==0) return;
    rep(i, h) {
      rep(j, w) {
        scanf("%d", &c[i][j]);
      }
    }

    int cnt = 0;
    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          search(i, j);
          cnt += 1;
        }
      }
    }

    cout << cnt << endl;
  }
}

void search(int x,int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0)
    return;

  c[x][y] = 0;

  for(int dx=-1; dx<=1; ++dx) {
    for(int dy=-1; dy<=1; ++dy) {
      search(x+dx, y+dy);
    }
  }
}