Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

#define PIAR pair<int, int>

struct pos{
    int x;
    int y;
};

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        stack<PIAR> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    st.push(PIAR(j, i));
                    field[i][j] = 0;
                    while(!st.empty()){
                    	int x = st.top().first;
                    	int y = st.top().second;
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            int nextX = x + dx[k];
                            int nextY = y + dy[k];
                            if(field[nextX][nextY] == 1){
                                st.push(PIAR(nextX, nextY));
                                field[nextX][nextY] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

C++: #include <cstdio>

int w, h;
int data[64][64];
int movex[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int movey[] = {-1, 0, 1, -1, 1, -1, 0, 1};

void dfs(int x, int y);

int main(void)
{	
	while (scanf("%d %d", &w, &h)){
		if (w == 0 && h == 0) break;
		
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				data[i][j] = 0;
			}
		}
		
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf("%d", &data[i][j]);
			}
		}
		
		int res = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (data[i][j] == 1){
					dfs(i, j);
					res++;
				}
			}
		}
		
		printf("%d\n", res);
	}
	
	return (0);
}

void dfs(int x, int y)
{
	data[x][y] = 0;
	
	for (int i = 0; i < 8; i++){
		if (data[x + movex[i]][y + movey[i]] == 1){
			dfs(x + movex[i], y + movey[i]);
		}
	}
	
	return;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//int cnt = 0;

void depth_search(int ,int,vector< vector<int> > &);
void show(vector<vector <int> > &umi){
  return ;
  cout << "---------------------------" << endl;
  for(int i = 0;i < umi[0].size();i++){
    for(int j = 0;j < umi.size();j++){
      cout << umi[j][i];
    }
    cout << endl;
  }
    cout << "---------------------------" << endl;
  
}
int main(){
  int w,h;
  while(true){
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0)return 0;
    vector< vector<int> > umi(w,vector<int>(h));
    //    cout << "aaaaaaaaaa" << endl;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	cin >> umi[j][i];

      }
    } 

    int cnt = 0;
    for(int i = 0;i < w;i++){
      for(int j = 0;j < h;j++){
	if( umi[i][j] == 1){
	  cnt++;
	  umi[i][j] = 3;
	  depth_search(i,j,umi);
	  show(umi);

	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void depth_search(int x,int y,vector< vector<int> > &umi){
  int dx[3] = {0,1,-1};
  int dy[3] = {0,1,-1};
  for(int i = 0;i < 3;i++){
    for(int j = 0;j < 3;j++){
      if(!(x+dx[i] == -1
	 ||x+dx[i] == umi.size()
	 ||y+dy[j] == -1
	 ||y+dy[j] == umi[0].size())
	 && (umi[x+dx[i]][y+dy[j]] == 1)
	 && !(dx[i] == 0 && dy[j] == 0)){
	umi[x+dx[i]][y+dy[j]] = 3;
	depth_search(x+dx[i],y+dy[j],umi);
	
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> P;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int w, h;
	while(cin >> w >> h, w|h){
		vector<vi> v(h, vi(w, 0));
		rep(i, h){
			rep(j, w){
				cin >> v[i][j];
			}
		}

		int ans = 0;
		rep(i, h){
			rep(j, w){
				if(v[i][j] != 1)continue;
				ans++;
				queue<P> q;
				q.push(MP(j,i));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					if(v[p.Y][p.X] != 1) continue;
					v[p.Y][p.X] = 0;
					rep(k, 8){
						int x = p.X+dx[k];
						int y = p.Y+dy[k];
						if(x < 0 || x >= w || y < 0 || y >= h) continue;
						q.push(MP(x, y));
					}
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
//using ll = long long;

int h, w;
vector< vector<int> > g;
//vector<vector<bool>> v;

void dfs(int y, int x) {
  g[y][x] = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int ny = y + i;
      int nx = x + j;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
      if (g[ny][nx] == 0) continue;
      dfs(ny, nx);
    }
  }
}

int main() {
  while (cin >> w >> h) {
    if (h == 0 || w == 0) break;
    g.assign(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) {
      cin >> g[i][j];
    }
    //v.assign(h, vector<bool>(w, false));
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] == 0) continue;
      dfs(i, j);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}