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

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

int main() {
	int w, h, ans, x, y;
	int c[52][52], f[52][52];
	queue< pair<int, int> > q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		for(int i=0; i<h+2; ++i) for(int j=0; j<w+2; ++j) {
			c[i][j] = 0; f[i][j] = 0;
		}
		ans = 0;
		
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(c[i][j]&&!f[i][j]) {
				ans++;
				q.push( pair<int, int>(i, j) );
				while(!q.empty()) {
					y = q.front().first; x = q.front().second;
					q.pop();
					f[y][x] = 1;
					for(int k=0; k<8; ++k) {
						if(c[y+dy[k]][x+dx[k]]&&!f[y+dy[k]][x+dx[k]]) {
							q.push( pair<int, int>(y+dy[k], x+dx[k]) );
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

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

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


void dfs(int x, int y){
	c[y][x] = 0;
	for(int i = 0; i < 8; i++){
		int mx = x + dx[i], my = y + dy[i];
		if( mx < 0 || my < 0 || w <= mx || h <= my ) continue;
		
		if( c[my][mx] == 1 ) dfs(mx, my);
	}
}

int main(){
	while( cin >> w >> h , w || h ){
		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				cin >> c[y][x];
			}
		}
		int ans = 0;
		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				if( c[y][x] == 1 ){
					ans++;
					dfs(x, y);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include<stdio.h>
int h,w;
int map[50][50];
void func(int ,int);
int main(void){
	while(1){
		scanf("%d %d",&w,&h);
		if(h==0 && w==0){
			break;
		}
		int i,j;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				scanf("%d",&map[i][j]);
			}
		}
		/*for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				printf("%d ",map[i][j]);
			}
			printf("\n");
		}*/
		int cnt=0;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(map[i][j]==1){
					map[i][j]=0;
					func(i,j);
					cnt++;
				}
			}
		}
		printf("%d\n",cnt);
		cnt=0;
	}
	return 0;
}
void func(int y,int x){
	int dy[8]={1,0,0,-1,-1,-1,1,1},dx[8]={0,1,-1,0,-1,1,-1,1};
	int i,j,xx,yy;
	for(i=0;i<8;i++){
		yy=y+dy[i];
		xx=x+dx[i];
		if(yy>=0 && yy<h && xx>=0 && xx<w && map[yy][xx]==1){
			map[yy][xx]=0;
			func(yy,xx);
		}
	}
}

C++: #include<bits/stdc++.h>

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

using namespace std;

int main(){

  while(1){
  
    int w,h;cin>>w>>h;if(w==0&&h==0){return 0;}

  int c[h+2][w+2];memset(c,0,sizeof(c));

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

  queue<pair<int,int> > q;

  bool a[h+2][w+2];

  for(int i=0;i<=h+1;i++){
    for(int j=0;j<=w+1;j++){
      a[i][j]=false;
    }
  }

  int ans=0;

  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      if(c[i][j]==1&&a[i][j]==false){
	ans++;
	q.push(make_pair(i,j));
	while(!q.empty()){
	  pair<int,int> tmp=q.front();q.pop();
	  int x=tmp.first;
	  int y=tmp.second;

	  for(int i=0;i<8;i++){
	    int checkx=dx[i]+x;
	    int checky=dy[i]+y;
	    if(a[checkx][checky]==false&&c[checkx][checky]==1){
	      a[checkx][checky]=true;
	      q.push(make_pair(checkx,checky));
	    }
	  }
	  
	}
      }
    }
  }

  cout<<ans<<endl;

  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const ll INF = 1LL<<60;


ll w,h;
const ll dx[]={-1,0,1,-1,1,-1,0,1},dy[]={-1,-1,-1,0,0,1,1,1};

// void dfs(ll height,ll width,ll map[height][width],ll check[height][width]){

// }

int main(){

  while(cin>>w>>h){
    if(w==0&&h==0)break;

    ll map[h][w];
    ll check[h][w];
    rep(i,0,h){
      rep(j,0,w){
        cin>>map[i][j];
        check[i][j]=-1;
      }
    }
    ll num=0;
    rep(i,0,h){
      rep(j,0,w){
        if(map[i][j]==1&&check[i][j]==-1){
          num++;
          stack<P>s;
          s.push(P(i,j));

          while(!s.empty()){
            P p=s.top();s.pop();
            ll y=p.first,x=p.second;

            check[y][x]=num;
            rep(i,0,8){
              ll ny=y+dy[i],nx=x+dx[i];
              if(IN(0,h,ny)&&IN(0,w,nx)&&map[ny][nx]==1&&check[ny][nx]==-1){
                s.push(P(ny,nx));
              }
            }
          }
        }
      }
    }

    out(num);

  }


  return 0;
}