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 <vector>
using namespace std;

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

int H,W;
vector<vector<int>> field;
int cnt;

bool seen[510][510];
void dfs(int h,int w){
  seen[h][w] = true;

  for(int dir=0;dir<8;dir++){
    int nh=h+dx[dir];
    int nw=w+dy[dir];

    if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
    if(field[nh][nw] == 0) continue;

    if(seen[nh][nw]) continue;

    dfs(nh,nw);
  }
}

int main(){
  cin >> W >> H;
  while(H!=0 || W!=0){
    field.resize(H);
    for(int i=0;i<H;i++){
      field[i].resize(W);
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        cin >> field[i][j];
      }
    }

    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        seen[i][j] = false;
      }
    }
    cnt = 0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(seen[i][j]) continue;
        if(field[i][j] == 1){
          dfs(i,j);
          cnt++;
        }
      }
    }

    cout << cnt << endl;
    cin >> W >> H;
  }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
using namespace std;

void solve(vector<vector<bool> > land)
{
	vector<vector<bool> > check(land.size(), vector<bool>(land[0].size(), false));

	int num = 0;
	for(unsigned i=1; i<land.size()-1; ++i){
		for(unsigned j=1; j<land[0].size()-1; ++j){
			if(!land[i][j] || check[i][j])
				continue;
			check[i][j] = true;
			++ num;

			queue<pair<int, int> > place;
			place.push(make_pair(i,j));
			while(!place.empty()){
				int y = place.front().first;
				int x = place.front().second;
				for(int dy=-1; dy<=1; ++dy){
					for(int dx=-1; dx<=1; ++dx){
						if(land[y+dy][x+dx] && !check[y+dy][x+dx]){
							check[y+dy][x+dx] = true;
							place.push(make_pair(y+dy, x+dx));
						}
					}
				}
				place.pop();
			}
		}
	}

	cout << num << endl;
}

int main()
{
	for(;;){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;

		vector<vector<bool> > land(h+2, vector<bool>(w+2, false));
		for(int i=1; i<=h; ++i){
			for(int j=1; j<=w; ++j){
				int tmp;
				cin >> tmp;
				land[i][j] = (tmp == 1);
			}
		}

		solve(land);
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[x][y] = '0';
  
  for(int dx = -1 ; dx <= 1 ; dx++) {
    for(int dy = -1 ; dy <= 1 ; dy++) {
      int nx = x + dx, ny = y + dy;
      if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  res++;
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

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

void dfs (vector<vector<int>> &vec, vector<vector<int>> &shd, int h, int w) {
  if (h < 0 || h >= vec.size() || w < 0 || w >= vec[0].size()) return;
  else if (!vec[h][w]) return;
  else if (shd[h][w]) return;
  else shd[h][w] = 1;

  dfs(vec, shd, h + 1, w);
  dfs(vec, shd, h , w + 1);
  dfs(vec, shd, h - 1, w);
  dfs(vec, shd, h, w - 1);
  dfs(vec, shd, h + 1, w + 1);
  dfs(vec, shd, h + 1, w - 1);
  dfs(vec, shd, h - 1, w + 1);
  dfs(vec, shd, h - 1, w - 1);

  return;
}
     
int main() {
  int w, h, cou=0;
  vector<int> vec_ans(1000,2147483647);

  cin >> w >> h;

  do {
    int ans=0;
    vector<vector<int>> vec(h, vector<int>(w, 0));
    vector<vector<int>> shd(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> vec[i][j];

    rep(i, h) rep(j, w) {
      if (!shd[i][j] && vec[i][j]) {
        ++ans;
        dfs(vec, shd, i, j);
      }
    }

    vec_ans[cou] = ans;
    ++cou;
    cin >> w >> h;
  } while (w || h);

  for (int i=0; vec_ans[i] != 2147483647; ++i) {
    cout << vec_ans[i] << endl;
  }
}

C++: #include<stdio.h>
#include<string.h>
#include<utility>
#include<iostream>
#include<queue>

using namespace std;
typedef long long ll;
 
#define REP(i,n) for(int i=0;i<(int)(n);i++)
 
int main()
{
    int ans,w,h;
    int c[55][55];
    queue<pair<int,int> > que;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={-1,0,1,1,1,0,-1,-1};
    while(1==1)
    {
        ans=0;
        REP(i,55)REP(j,55)c[i][j]=10000;
        scanf("%d %d\n",&w,&h);
        if(w==0&&h==0)return 0;
        REP(i,h)REP(j,w)scanf("%d",&c[i+1][j+1]);
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(c[i][j]==1)
                {
                    ans++;
                    c[i][j]=0;
                    while(!que.empty())que.pop();
                    que.push(pair<int,int>(i,j));
                    pair<int,int> p;
                    while(!que.empty())
                    {
                        p=que.front();
                        que.pop();
                        REP(k,8)
                        {
                            if(c[p.first+dy[k]][p.second+dx[k]]==1)
                            {
                                que.push(pair<int,int>(p.first+dy[k],p.second+dx[k]));
                                c[p.first+dy[k]][p.second+dx[k]]=0;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}