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 <queue>
#include <utility>

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0);

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

    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        cin >> map[i][j];
      }
    }

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }
  }
}

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

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<pos> 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){
                    pos p;
                    p.y = i;
                    p.x = j;
                    st.push(p);
                    field[i][j] = 0;
                    while(!st.empty()){
                        pos stTop = st.top();
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            p = stTop;
                            p.y += dy[k];
                            p.x += dx[k];
                            if(field[p.y][p.x] == 1){
                                st.push(p);
                                field[p.y][p.x] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

int w;
int h;
bool a[100][100];
int counter = 0;
int hor[3] = {-1,0,1};
int ver[3] = {-1,0,1};
int cnt = 0;
int i;
int j;
int i2;
int j2;

void dfs(int row,int col){
  if(row>=h || col>=w || row<0 || col<0 || a[row][col]==0){
    return;
  }
  a[row][col] = 0;
  for(int i2=0;i2<3;i2++){
    for(int j2=0;j2<3;j2++){
      dfs(row+hor[i2],col+ver[j2]);
    }
  }
}
int main() {
  while(1){
    cnt = 0;
    cin >>w;
    cin >>h;
    if(w==0 || h==0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
      cin>>a[i][j];
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(a[i][j]==1){
          cnt++;
          dfs(i,j);
        }
      }
    }
    printf("%d\n",cnt);
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

vector< vector<int> > M;
int h,w;

bool dfs(int y, int x){
    if(y>=h||y<0||x>=w||x<0)return false;
    if(M[y][x]==0)return false;
    M[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            dfs(y+i,x+j);
        }
    }
    return true;
}

int ans(){
    int res=0;
    REP(i,h)REP(j,w){
        if(dfs(i,j))res++;
    }
    
    return res;
}

int main(){
    
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        
        M.clear();
        M.resize(h);
        REP(i,h){
            REP(j,w){
                int t; cin>>t;
                M[i].push_back(t);
            }
        }
        
        cout<<ans()<<endl;
        
    }
    
    return 0;
}

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

using namespace std;

struct UFTree{
	vector<int> p;
	UFTree(int n):p(n,-1){}
	void Unite(int a,int b){
		int ra=Find(a),rb=Find(b);
		if(ra!=rb){
			if(-p[ra]<-p[rb])
				swap(ra,rb);
			p[ra]+=p[rb];
			p[rb]=ra;
		}
	}
	int Find(int n){
		return p[n]<0?n:p[n]=Find(p[n]);
	}
};

int main()
{
	for(int w,h;cin>>w>>h,w|h;){
		int grid[50][50];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				cin>>grid[i][j];
		UFTree uft(h*w);
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++){
				if(!grid[i][j])
					continue;
				int di[]={0,1,1,1},dj[]={1,-1,0,1};
				for(int k=0;k<4;k++){
					int ni=i+di[k],nj=j+dj[k];
					if(0<=ni && ni<h && 0<=nj && nj<w && grid[ni][nj])
						uft.Unite(i*w+j,ni*w+nj);
				}
			}
		int res=0;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				if(grid[i][j] && uft.p[i*w+j]<0)
					res++;
		cout<<res<<endl;
	}
	return 0;
}