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;

void dfs(int i, int j, const vector<vector<int> >& c, vector<vector<int> >& arrived, int count) {
    arrived[i][j] = count;

    if (c[i + 1][j] == 1 && arrived[i + 1][j] == 0) {
        dfs(i + 1, j, c, arrived, count);
    }
    if (c[i - 1][j] == 1 && arrived[i - 1][j] == 0) {
        dfs(i - 1, j, c, arrived, count);
    }
    if (c[i][j + 1] == 1 && arrived[i][j + 1] == 0) {
        dfs(i, j + 1, c, arrived, count);
    }
    if (c[i][j - 1] == 1 && arrived[i][j - 1] == 0) {
        dfs(i, j - 1, c, arrived, count);
    }
    if (c[i + 1][j + 1] == 1 && arrived[i + 1][j + 1] == 0) {
        dfs(i + 1, j + 1, c, arrived, count);
    }
    if (c[i + 1][j - 1] == 1 && arrived[i + 1][j - 1] == 0) {
        dfs(i + 1, j - 1, c, arrived, count);
    }
    if (c[i - 1][j + 1] == 1 && arrived[i - 1][j + 1] == 0) {
        dfs(i - 1, j + 1, c, arrived, count);
    }
    if (c[i - 1][j - 1] == 1 && arrived[i - 1][j - 1] == 0) {
        dfs(i - 1, j - 1, c, arrived, count);
    }
}

void Main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

        vector<vector<int> > c(h + 2, vector<int>(w + 2, 0));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                int ct;
                cin >> ct;
                c[i + 1][j + 1] = ct;
            }
        }

        vector<vector<int> > arrived(h + 2, vector<int>(w + 2, 0));
        int count = 0;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (c[i][j] == 1 && arrived[i][j] == 0) {
                    ++count;
                    dfs(i, j, c, arrived, count);
                }
            }
        }

        set<int> ans;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (arrived[i][j] > 0) {
                    ans.insert(arrived[i][j]);
                }
            }
        }
        cout << ans.size() << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<cstdlib>


#define rep(n,cnt) for(int cnt=0;cnt<n;++cnt)

using namespace std;

template <typename TYPE>
void printArray(TYPE* array, int size){
  rep(size,i) cout << array[i] << "\t" << flush;
  cout << endl;
}

int StringToInt(string str){
  return atoi(str.c_str());
}

#define MAX_W 50
#define MAX_H 50

class State{
public:
  int y;
  int x;
  int island_count;
};

int w,h,c[MAX_W][MAX_H];
int count = 0;

void dfs(int sy, int sx){
  c[sy][sx] = 0;

  for(int vy=-1;vy<=1;++vy){
    for(int vx=-1;vx<=1;++vx){
      int y = sy + vy;
      int x = sx + vx;
      if(c[y][x] == 1) dfs(y,x);
    }
  }
}

int main(){
  while(true){
    count = 0;
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0) break;
    rep(h,y){
      rep(w,x){
	cin >> c[y][x];
      }
    }
    
    rep(h,y){
      rep(w,x){
	if(c[y][x] == 1){
	  dfs(y,x);
	  count++;
	}
      }
    }
    cout << count << endl;
  }
}

C++: #include <iostream>
#include <cstring>

using namespace std;


int W, H;
int map[55][55];



void dfs(int cx, int cy) {
	map[cy][cx] = 0;
	
	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			if (map[cy + dy][cx + dx] == 1) dfs(cx + dx, cy + dy);
		}
	}
}

int solver() {
	int 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++;
			}
		}
	}
	
	return cnt;
}

int main() {
	
	while (1) {
		cin >> W >> H; if (W==0 && H==0) break;
		
		memset(map, 0, sizeof(map));
		for (int i=0; i<H; i++)
			for (int j=0; j<W; j++) cin >> map[i][j];
		
		cout << solver() << endl;
	}
	
	return 0;
}

C++: #include <iostream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repi(i, a, n) for(int i = a; i < (n); ++i)

int w, h;
int island[100][100];

void dfs(int x, int y) {
  island[y][x] = 0;

  repi(mat_y, -1, 2) {
    repi(mat_x, -1, 2) {
      if (x + mat_x < 0 || y + mat_y < 0 || x + mat_x >= w || y + mat_y >= h) continue;
      if (island[y + mat_y][x + mat_x]) dfs(x + mat_x, y + mat_y);
    }
  }
}

int main(){
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    rep(y, h) {
      rep(x, w) {
        cin >> island[y][x];
      }
    }
    int cnt = 0;
    rep(y, h) {
      rep(x, w) {
        if (island[y][x]) {
          ++cnt;
          dfs(x, y);
        }
      }
    }
    cout << cnt << endl;
  }
}

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

// How Many Islands?(1160)
const int dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void solve(vector<vector<int> >&f, int x, int y)
{
	if (x < 0 || x >= (int)f[0].size() || y < 0 || y >= (int)f.size() || f[y][x] == 0)
		return;

	f[y][x] = 0;
	for (int i = 0; i < 8; ++i)
		solve(f, x+dx[i], y+dy[i]);
	return;
}

int main()
{
	int w, h;
	while (cin >> w >> h) {
		if (w == 0 && h == 0)
			break;

		vector<vector<int> > f(h, vector<int>(w));
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				cin >> f[i][j];
		}

		int ans = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (f[i][j]) {
					solve(f, j, i);
					++ans;
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}