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>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

void paint(vector<string> &board, int y, int x) {
	queue<int> q;
	q.push(y * 100 + x);
	board[y][x] = '0';
	while (!q.empty()) {
		int now = q.front(); q.pop();

		int nowX = now % 100;
		int nowY = now / 100;

		for (int ay = -1; ay <= 1; ay++) {
			for (int ax = -1; ax <= 1; ax++) {
				int nx = nowX + ax;
				int ny = nowY + ay;
				if (nx < 0 || nx >= board[0].size() || ny < 0 || ny >= board.size()) continue;
				if (board[ny][nx] == '0') continue;

				q.push(ny * 100 + nx);
				board[ny][nx] = '0';
			}
		}
	}
}

signed main() {
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0) break;

		vector<string> board;
		rep(y, h) {
			string tmp = "";
			rep(x, w) {
				tmp += nextChar();
			}
			board.push_back(tmp);
		}

		int ret = 0;
		rep(y, h) {
			rep(x, w) {
				if (board[y][x] == '1') {
					paint(board, y, x);
					ret++;
				}
			}
		}

		cout << ret << endl;
	}

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

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

typedef long long ll;

#define MAX 100

int w, h;
int t[MAX][MAX];

int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1,1,-1, 0, 1, -1,  1, 0 };

bool destroy(int x, int y){
  if(t[y][x] == 0) return false;
  t[y][x] = 0;
  rep(i, 8){
    int nx = x + dx[i], ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    destroy(nx, ny);
  }
  return true;
}

void solve(){
  int res = 0;
  rep(i,h) rep(j,w) if(destroy(j,i)) res++;
  cout << res << endl;
}
int main(){
  while(cin >> w >> h){
    if( w==0 && h ==0) break;
    rep(i, h) rep(j, w) cin >> t[i][j];
    solve();
  }
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-23T00:13:28+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int w,h;
int c[55][55];
bool seen[55][55];
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};
void dfs(int y,int x){
    seen[y][x] = true;

    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
        if(seen[ny][nx] == true) continue;
        if(c[ny][nx] == 0) continue;

        dfs(ny,nx);
    }

    return;
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

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

      rep(i,h)rep(j,w) cin >> c[i][j];
      memset(seen, false, sizeof(seen));
      int cnt = 0;
      rep(i,h)rep(j,w){
          if(seen[i][j]==false && c[i][j] == 1){
              dfs(i,j);
              cnt++;
          }
      }

      cout << cnt << endl;
  }

  return 0;
}
/********  Main() Ends Here *************/

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int MAX = 53;
int W, H;
int field[MAX][MAX];

int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};


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

  rep(i,8) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || H <= nx) continue;
    if (ny < 0 || W <= ny) continue;
    if (field[nx][ny] == 0) continue;
    dfs(nx, ny);
  }
}

int main() {
  vector<int> ans;
  while (true) {
    int cnt = 0;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    rep(i,H) rep(j,W) cin >> field[i][j];
    
    rep(i,H) rep(j,W) {
      if (field[i][j] == 1) {
	dfs(i, j);
	cnt++;
      }
    }
    ans.push_back(cnt);
  }
  rep(i,ans.size()) {
    cout << ans[i] << endl;
  }
  return 0;
}