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<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
 
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
 
using namespace std;
 
 int dx[8] = {1,0,-1,0,1,1,-1,-1};
 int dy[8] = {0,1,0,-1,-1,1,-1,1};

void dfs(vector<vector<int> > &map,int x,int y,int h, int w){
  
    if(x < 0 || x >=w || y < 0 || y >=h){
      return ;
    }

    if(map[y][x]==0){
      return;
    }

    map[y][x] = 0;

    for(int i=0;i<8;i++){
      dfs(map,x+dx[i],y+dy[i],h,w);
    }

    return;
}


int main()
{

  while(true){

    int w,h;
    cin >> w >> h;

    if(w==0&&h==0){
      break;
    }

    vector<vector<int> > map(h,vector<int>(w,0));    
    int count=0;

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


   for(int i=0;i<w;i++){
    for(int k=0;k<h;k++){
      if(map[k][i]==1){
        dfs(map,i,k,h,w);
        count++;
      }
    }
   }
 


    cout << count << endl;

  }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
int w,h,fld[64][64];

void dfs(int y,int x){
    if(x<0||x>=w||y<0||y>=h)return;
    if(!fld[y][x])return;
    fld[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i||j)dfs(y+i,x+j);
        }
    }
}
int main(){
    while(scanf("%d%d",&w,&h),w||h){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&fld[i][j]);
            }
        }

        int cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(fld[i][j]){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][52],int b[][52],int x,int y){
  if(a[x][y]==0||b[x][y]==1)
    return 0;
  else{
    b[x][y]=1;
    f(a,b,x-1,y-1);
    f(a,b,x,y-1);
    f(a,b,x+1,y-1);
    f(a,b,x-1,y);
    f(a,b,x+1,y);
    f(a,b,x-1,y+1);
    f(a,b,x,y+1);
    f(a,b,x+1,y+1);
    return 1;
  }
}
int main(){
  int i,j;
  int a[52][52],b[52][52],w,h;
  int ct;
  while(cin>>w>>h&&w+h){
    memset(a,0,sizeof(a));
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	cin>>a[j][i];
    memset(b,0,sizeof(b));
    ct=0;
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	ct+=f(a,b,j,i);
    cout<<ct<<endl;
  }
  return 0;
}

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

using namespace std;

int main()
{
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            return 0;
        }
        vector<vector<int>> v(h, vector<int>(w, -1));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> v[i][j];
            }
        }
        constexpr int MIN_ID = 10;
        function<int(int, int, int)> f = [&v, &f, w, h, MIN_ID](int i, int j,
                                                                int id) {
            // ?????¢????¶???????
            if (i == -1 || j == -1 || i == h || j == w) {
                return 0;
            }
            // ?????§?????¶????±?????????????
            else if (MIN_ID <= v[i][j] && v[i][j] <= id) {
                return 0;
            }
            // ???
            else if (v[i][j] == 0) {
                return 0;
            }
            else {
                v[i][j] = id;
                int r = 1;
                r += f(i - 1, j - 1, id);
                r += f(i - 1, j, id);
                r += f(i - 1, j + 1, id);
                r += f(i, j - 1, id);
                r += f(i, j + 1, id);
                r += f(i + 1, j - 1, id);
                r += f(i + 1, j, id);
                r += f(i + 1, j + 1, id);
                return r;
            }
        };
        int islandId = MIN_ID;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                auto r = f(i, j, islandId);
                if (r > 0) {
                    islandId += 1;
                }
            }
        }

        cout << (islandId - MIN_ID) << endl;
    }
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                cin >> m[i][j];

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
                    isl++;
                    dfs(i, j);
                }
            
        cout << isl << endl;
    }

    return 0;
}