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;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w != 0 && h != 0){
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define MAX 70
#define rep(i,p) for(int i=0;i<p;i++)
#define pb push_back
using namespace std;
int w,h,ans,yoko,tate;
int hoge;
bool c[MAX][MAX];
bool visit[MAX][MAX];
// ?§???? {?????????????????????????????????????????????????????????}
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,1,-1};

bool dfs (int x, int y) {
  if(!c[x][y]) return false;
  visit[x][y] = true;
  rep(i,8) {
    yoko = x+dx[i];
    tate = y+dy[i];
    if ( c[yoko][tate] and !visit[yoko][tate] ) dfs(yoko,tate);
  }
  return true;
}
int main(){
  while (1) {
    cin >> w >> h;
    if (w == 0 and h == 0) break;
    rep(i,MAX) rep(j,MAX) c[i][j] = false;
    rep(i,MAX) rep(j,MAX) visit[i][j] = false;
    ans = 0;
    // ?????? 0 false
    // ?????? 1 true
    // ??§?¨? [1,w]x[1,h]????????´??????(false)
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++) {
        cin >> hoge;
        if (hoge == 1) c[i][j] = true;
      }
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
        if (!visit[i][j])
          // dfs ????????¢?´¢???????????\???????????¨??????false?????????
          if (dfs(i,j)) ans++;
    cout << ans << endl;
  }
  return 0;
}

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

int w,h;
int maps[50][50];

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1 && xx <= 0 && xx < w && yy <= 0 && yy < h) dfs(yy,xx);
    }
  }
}

int main(){
  while(cin >> w >> h && w)
    {
      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  cin >> maps[i][j];
	}
      }
      int island=0;

      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  if(maps[i][j] == 1){
	    island++;
	    dfs(i,j);
	  }
	}
      }
      cout << island << endl;
    }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;

vector<int>dx={-1,-1,-1,0,0,1,1,1};
vector<int>dy={-1,0,1,-1,1,-1,0,1};

void dfs(vector<vector<int>>&c,vector<vector<int>>&d, int x, int y,int w,int h){
    d[x][y]=1;
    for(int i=0;i<8;i++){
        int newx=x+dx[i];
        int newy=y+dy[i];
        if(newx<0 || newx>=h || newy<0 || newy>=w)continue;
        if(d[newx][newy]==0 && c[newx][newy]==1){
            dfs(c,d,newx,newy,w,h);
        }
    }
}

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


    return 0;
}

C++: // Tazoe

#include <iostream>
using namespace std;

void DFS(int M[52][52], int y, int x)
{
	M[y][x] = 0;

	if(M[y-1][x-1]==1)
		DFS(M, y-1, x-1);
	if(M[y-1][x]==1)
		DFS(M, y-1, x);
	if(M[y-1][x+1]==1)
		DFS(M, y-1, x+1);
	if(M[y][x-1]==1)
		DFS(M, y, x-1);
	if(M[y][x+1]==1)
		DFS(M, y, x+1);
	if(M[y+1][x-1]==1)
		DFS(M, y+1, x-1);
	if(M[y+1][x]==1)
		DFS(M, y+1, x);
	if(M[y+1][x+1]==1)
		DFS(M, y+1, x+1);
}

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

		if(w==0&&h==0)
			break;

		int M[52][52];
		for(int y=0; y<=h+1; y++)
			for(int x=0; x<=w+1; x++)
				M[y][x] = 0;

		for(int y=1; y<=h; y++)
			for(int x=1; x<=w; x++)
				cin >> M[y][x];

		int cnt = 0;

		for(int y=1; y<=h; y++)
			for(int x=1; x<=w; x++)
				if(M[y][x]==1){
					DFS(M, y, x);

					cnt++;
				}

		cout << cnt << endl;
	}

	return 0;
}