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 <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

int w,h;

int main()
{
    while(cin >> w >> h,w)
    {
        vector<vector<int>> fie(h,vector<int>(w,0));
        rep(i,0,h - 1)rep(j,0,w - 1) cin >> fie[i][j];
        int res = 0;
        rep(i,0,h - 1)rep(j,0,w - 1){
            if(fie[i][j])
            {
                queue<P> que;
                res++;
                que.push({i,j});
                fie[i][j] = 0;
                while(!que.empty())
                {
                    auto p = que.front();
                    que.pop();

                    for(int i = p.first - 1;i <= p.first + 1;i++)
                    {
                        for(int j = p.second - 1;j <= p.second + 1;j++)
                        {
                            if(0 <= i && i < h && 0 <= j && j < w && fie[i][j])
                            {
                                que.push({i,j});
                                fie[i][j] = 0;
                            }
                        }
                    }
                }
            }
        }
        cout << res << endl;
    }
}

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

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

void dfs(int y, int x, vector< vector<int> > &v){
    v[y][x] = 0;

    for(int i = 0 ; i < 8 ; i++){
        int ny = y + dy[i], nx = x + dx[i];
        if(v[ny][nx] == 1){
            dfs(ny, nx, v);
        }
    }

    return;
}

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

        int ans = 0;
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                if(c[i][j] == 1){
                    ans++;
                    dfs(i, j, c);
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;

int main(){
  int w,h;
  int dx[8] = {1,1,0,-1,-1,-1,0,1};
  int dy[8] = {0,1,1,1,0,-1,-1,-1};
  while(true){
  cin>>w>>h;
  if(!w&&!h)break;
  w+=2,h+=2;
  int island[w][h];
  for(int x=0;x<w;x++){
    island[x][0] = 0;
    island[x][h-1] = 0;
  }
  for(int y=1;y<h-1;y++){
    island[0][y] = 0;
    island[w-1][y] = 0;
    for(int x=1;x<w-1;x++){
      cin>>island[x][y];
    }
  }
  int count = 0;
  queue<int> qu;
  for(int y=1;y<h-1;y++){
    for(int x=1;x<w-1;x++){
      if(island[x][y]){
	qu.push(y*w+x);
	while(!qu.empty()){
	  if(island[qu.front()%w][qu.front()/w]){
	    island[qu.front()%w][qu.front()/w] = 0;
	    for(int i=0;i<8;i++)
	      qu.push(qu.front()+dy[i]*w+dx[i]);
	  }
	  qu.pop();
	}
	count++;
      }
    }
  }
  cout<<count<<endl;
  }
  return 0;
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+1, y);
    dfs(x+1, y+2);
    dfs(x+1, y-2);
    dfs(x-1, y);
    dfs(x-1, y+2);
    dfs(x-1, y-2);
    dfs(x, y+2);
    dfs(x, y-2);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            cin.getline(c[k], sizeof(c[k]));
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define X first
#define Y second

using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int cnt, w, h;
int m[100][100];
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void dfs(pii);

int main(void){
    for(;cin >> w >> h, w; cout << cnt << endl){
        cnt = 0;
        rep(y, h) rep(x, w) cin >> m[y][x];
        rep(y, h) rep(x, w) if(m[y][x] == 1) cnt ++, dfs(mp(x, y));
    }
 
    return 0;
}

void dfs(pii p){
    if(p.X < 0 || w <= p.X ||
       p.Y < 0 || h <= p.Y) return;
    
    if(m[p.Y][p.X] == 1) m[p.Y][p.X] = 0;
    else return;

    rep(i, 8) dfs(mp(p.X + dx[i], p.Y + dy[i]));
}

C++: #include <bits/stdc++.h>
using namespace std;
bool island[50][50];
int n,m;
int dx[9]={0,1,1,1,0,-1,-1,-1},dy[9]={-1,-1,0,1,1,1,0,-1};
void serch(int y,int x)
{
	island[y][x]=0;
	for(int i=0,ax,ay;i<9;i++)if((ay=dy[i]+y)<m&&(ax=dx[i]+x)<n&&ay>=0&&ax>=0&&island[ay][ax])serch(ay,ax);
}
int main() {
	while(cin>>n>>m,n,m){
		int ans=0;
		for(int i=0;i<m;i++)for(int j=0;j<n;j++)cin>>island[i][j];
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if(island[i][j]){
					ans++;
					serch(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
}