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(i, j));
                    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 <bits/stdc++.h>
#define rep(i,s,n) for(int i=s;i<n;i++)
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
using P=pair<int,int>;
using G=vector<vector<int>>;
using ll=long long;

string subsum(int* A,int n,int m){
  bitset<2010> E(1);

  rep(i,0,n){
    E |= (E<<A[i]);
  }

  if(E.test(m)) return "yes";
  else return "no";
}

vector<bool> seen;
vector<int> d;
vector<int> f;

int H,W;
vector<vector<int>> grid;

void dfs(int h, int w){
  grid[h][w]=0;

  for(int dh=-1;dh<=1;dh++){
    for(int dw= -1;dw<=1;dw++){
      int nh=h+dh; int nw=w+dw;

      if(nh<0||nw<0||nh>=H||nw>=W) continue;
      //探索済みまたは海
      if(grid[nh][nw]==0) continue;

      dfs(nh,nw);
    }
  }
}

int main(int argc,char **argv){
  while(1){
    cin>>W>>H;
    if(W==0 && H==0) break;

    grid.assign(H,vector<int>(W,0));
    rep(h,0,H){
      rep(w,0,W) cin>>grid[h][w];
    }

    int cnt=0;
    rep(h,0,H){
      rep(w,0,W){
        if(grid[h][w]==0) continue;
        dfs(h,w);
        cnt++;
      }
    }

    cout<<cnt<<endl;


  }
  }

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

int CountIsland(int w,int h,int c[][50]);
void CheckIsland(int w,int h,int c[][50]);
void debugg(int w,int h,int c[][50]);
int main(){
	int w,h;
	int c[50][50];
	while(true){
		for(int i=0;i<50;i++){
			for(int j=0;j<50;j++){
				c[i][j]=-1;
			}
		}
		cin >> w >> h;
		if(w==0&&h==0){break;}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> c[i][j];
			}
		}
		cout << CountIsland(w,h,c) << endl;
	}
	return 0;
}
int CountIsland(int w,int h,int c[][50]){
	int r=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]==1){
				r++;
				CheckIsland(j,i,c);
			}
		}
	}
	return r;
}
void debugg(int w,int h,int c[][50]){
	cout << endl;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cout << c[i][j] << " ";
		}
		cout << endl;
	}
}
void CheckIsland(int w,int h,int c[][50]){
	c[h][w] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i == -1 && h == 0 || j == -1 && w == 0 || i == 1 && h == 49 || j == 1 && w == 49||i == 0 && j == 0){continue;}
			if(c[i+h][j+w]==1){
				CheckIsland(j+w,i+h,c);
			}
		}
	}
}

C++: #include<iostream>
#define N 51
using namespace std;

int w, h, ans = 0, map[N][N];

void search_zahyou(int w, int h);

int main(){
	while(cin >>w >>h){
		ans = 0;
		if(w == 0 && h == 0) break;
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				cin >>map[i][k];
			}
		}
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				if(map[i][k] == 0){
					continue;
				}
				else{
					ans++;
					search_zahyou(i, k);
				}
			}
		}
		cout <<ans <<endl;
	}
	return 0;
}

void search_zahyou(int inh, int inw){
	int searchh[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, searchw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
	if(h <= inh || w <= inw || inh < 0 || inw < 0){
	} else{
		map[inh][inw] = 0;
		for(int i = 0; i < 8; i++){
			if(map[inh + searchh[i]][inw + searchw[i]] == 1){
				search_zahyou(inh + searchh[i], inw + searchw[i]);
			}
		}
	}
}

C++: #include <iostream>

using namespace std;

int w,h;
int dx[8] = { -1 , -1 , -1 ,  0 , 0 ,  1 , 1 , 1};
int dy[8] = { -1 ,  0 ,  1 , -1 , 1 , -1 , 0 , 1};
int map[51][51];

void check(int x , int y){
	int mx,my;

	for(int i=0 ; i<8 ; i++){//üÍWûüð²×é
		mx = x + dx[i];
		my = y + dy[i];

		//tB[hOÉoÄ¢È¯êÎ²×é(0ÈãE³Èà)
		if(mx>=0 && mx<w && my>=0 && my<h){
			if(map[my][mx]==1){//¾Á½ç
				map[my][mx] = 2;//ñdÉ²×È¢½ßÉÊÌlðüêé
				check( mx , my );
			}
		}
	}
}

int main(){
	int count;//ÌðJEg·éÏ

	while(1){
		//ÏÌú»
		count = 0;
		
		//E³ÌüÍ
		cin >> w >> h;
		if(w==0 && h==0) break;//0 0ªüÍ³ê½çI¹

		//tB[hª©C©ÌîñðüÍ
		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				cin >> map[y][x];
			}
		}

		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				if(map[y][x]==1){//¾Á½çÄAÖðÄÑo·
					map[y][x] = 2;
					count++;
					check( x , y );
				}
			}
		}

		//¦ÌoÍ
		cout << count << endl;
	}

	return 0;
}