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 <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

ll gcd(ll a, ll b) {
  ll tmp;
  if ( b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b !=0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

ll lcm(ll a, ll b) {
  return a * b / gcd(a, b);
}

int d[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int w, h;
void dfs(int y, int x, vector<vector<int>> &v) {
  v[y][x] = 0;
  for(int i = 0; i<8; i++) {
    int ny = y + d[i][0];
    int nx = x + d[i][1];
    if(0<=ny and ny<h and 0<=nx and nx<w and v[ny][nx] == 1) {
      dfs(ny, nx, v);
    }
  }
}

int main(void) {
  while(1) {
    cin>>w>>h;
    if(w == 0 and h == 0)
      break;
    vector<vector<int>> v;
    for(int i = 0; i<h; i++) {
      vector<int> vv;
      for(int j=0; j<w; j++) {
	int x;
	cin>>x;
	vv.push_back(x);
      }
      v.push_back(vv);
    }
    int cnt = 0;
    for(int i=0; i<h; i++) {
      for(int j=0; j<w; j++) {
	if (v[i][j] == 1) {
	  cnt++;
	  dfs(i, j, v);
	}

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

C++: // https://onlinejudge.u-aizu.ac.jp/challenges/search/titles/1160
// dfs

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>

using namespace std;

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

typedef long long ll;

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

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

void Main()
{
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        memset(field, 0, sizeof(field));
        rep(i, H)
        {
            rep(j, W)
            {
                cin >> field[i][j];
            }
        }
        int cnt = 0;
        rep(i, H)
        {
            rep(j, W)
            {
                if (field[i][j] == 1) {
                    dfs(i, j);
                    ++cnt;
                }
            }
        }
        cout << cnt << endl;
    }
}

int main()
{
    // cin.tie(nullptr);
	// ios_base::sync_with_stdio(false);
	Main();
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> c;
long long dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long dy[8] = {-1, 0, 1, 1, -1, -1, 0, 1};
long long W, H;

void DFS(long long y, long long x)
{
    c[y][x] = 0;
    for (long long i = 0; i < 8; i++)
    {
        long long tx = x + dx[i];
        long long ty = y + dy[i];

        if (tx < 0 || ty < 0 || tx >= W || ty >= H)
            continue;

        if (c[ty][tx] == 0) //if(tx~)と逆だとover
            continue;

        DFS(ty, tx);
    }
}

int main()
{

    while (cin >> W >> H)
    {
        if (H == 0 && W == 0)
            break;
        long long cnt = 0;
        c.assign(H, vector<long long>(W, 0));
        //bool seen[H][W] = {false};

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                cin >> c[i][j];
            }
        }

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                //if (c[i][j] == 1) この条件だと,海の近くに島があるとそれを塗りつぶしてしまう。
                //    cnt++;
                if (c[i][j] == 0) //この条件を入れないと,海隣の島を塗りつぶしてしまう。
                    continue;
                DFS(i, j);
                cnt++;
            }
        }

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

C++: #include <bits/stdc++.h>

using namespace std;

#define llong long long 
int w,h;
int sum;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int H, int W){
	if(area[H][W] == 0)return;
	area[H][W] = 0;
	for(int i = 0; i < 8; i++){
		int sh = H + nh[i];
		int sw = W + nw[i];
		if(sh < 0 || sw < 0 || sh >= h || sw >= w) continue;
		solve(sh, sw);
	}

	return;
}

int main(){

	while(cin >> w >> h, w){
		sum = 0;
		fill_n(*area, 50 * 50, 0);
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}	

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(area[i][j] == 1){
					sum++;
					solve(i, j);
				}
			}
		}

		cout << sum << endl;
	}
	return 0;
}