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<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define N (1000000000+7)
typedef pair<int,int> P;
typedef long long ll;

int h,w;
int board[60][60];
bool check(int i,int j){
    return (i>=1)&&(i<=h)&&(j>=1)&&(j<=w)&&(board[i][j]==1);
}

void dfs(int i,int j){
    if(check(i,j)){
        board[i][j]=-1;
        for(int x=i-1;x<=i+1;++x){
            for(int y=j-1;y<=j+1;++y){
                dfs(x,y);
            }
        }
    }
    else return;
}

int main(void){
    while(true){
        cin>>w>>h;
        int sx=0,sy=0;
        int ans=0;
        if((h==0)&&(w==0))break;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>board[i][j];
            }
        }
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(board[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<int> xw = {-1,  0,  1, -1,  1, -1,  0,  1 };
vector<int> yh = {-1, -1, -1,  0,  0,  1,  1,  1 };
void dfs( vector<vector<int>> &vec, int X, int Y, int &w, int &h, vector<vector<bool>> &ck ){
  if ( ck.at(Y).at(X) ) return;
  
  ck.at(Y).at(X) = true;
  for (int i=0; i<8; i++) {
    int x = X + xw.at(i);
    int y = Y + yh.at(i);
    if( x < 0 || w <= x ) continue;
    if( y < 0 || h <= y ) continue;
    if ( vec.at(y).at(x) ) dfs( vec, x, y, w, h, ck);
  }
}

int main() {
  
  while (true) {
  int W,H;
  cin >> W >> H;
  if( W == 0 || H == 0) break;
    
  vector<vector<int>> P(H, vector<int>(W));
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      cin >> P.at(i).at(j);  
    }
  }
  
  vector<vector<bool>> checked(H, vector<bool>(W, false));
  int res = 0;
  for (int h=0; h<H; h++) {
    for (int w=0; w<W; w++) {
      if( !checked.at(h).at(w) && P.at(h).at(w) ) {
         res++; dfs( P, w, h, W, H, checked);
      }
    }
  }
  
  cout << res << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;

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

void search(int i, int j){
    if(i-1 >= 0 && j-1 >= 0 && c[i-1][j-1] == 1){
        c[i-1][j-1] = 0;
        search(i-1, j-1);
    }
    if(j-1 >= 0 && c[i][j-1] == 1){
        c[i][j-1] = 0;
        search(i, j-1);
    }
    if(i+1 < h && j-1 >= 0 && c[i+1][j-1] == 1){
        c[i+1][j-1] = 0;
        search(i+1, j-1);
    }
    if(i-1 >= 0 && c[i-1][j] == 1){
        c[i-1][j] = 0;
        search(i-1, j);
    }
    if(i+1 < h && c[i+1][j] == 1){
        c[i+1][j] = 0;
        search(i+1, j);
    }
    if(i-1 >= 0 && j+1 < w && c[i-1][j+1] == 1){
        c[i-1][j+1] = 0;
        search(i-1, j+1);
    }
    if(j+1 < w && c[i][j+1] == 1){
        c[i][j+1] = 0;
        search(i, j+1);
    }
    if(i+1 < h && j+1 < w && c[i+1][j+1] == 1){
        c[i+1][j+1] = 0;
        search(i+1, j+1);
    }
    return;
}

int main()
{
    int ans;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        // init
        ans = 0;
        c.resize(h);
        for(int i=0; i<h; ++i){
            c[i].resize(w);
            for(int j=0; j<w; ++j){
                cin >> c[i][j];
            }
        }
        // search
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(c[i][j] == 1){
                    ans++;
                    c[i][j] = 0;
                    search(i, j);
                }
            }
        }
        // output
        cout << ans << endl;
        // clear
        for(int i=0; i<h; ++i){
            c[i].clear();
        }
        c.clear();
    }
    return 0;
}

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

int field[50+2][50+2];
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};

int rec(int x, int y)
{
  int i;

  if(field[x][y]!=1) return 0;
  field[x][y] = 0;
  for(i=0;i<8;i++) rec(x+dx[i],y+dy[i]);
  return 1;
}

int main()
{
  int w,h;
  int i,j;
  int n;
  while(cin>>w>>h,w|h){
    for(j=1;j<=h;j++)
      for(i=1;i<=w;i++)
	cin>>field[i][j];
    for(j=1;j<=h+1;j++)
      field[w+1][j]=0;
    for(i=1;i<=w+1;i++)
      field[i][h+1]=0;

    n = 0;
    for(j=1;j<=h;j++) for(i=1;i<=w;i++) n += rec(i,j);

    cout<<n<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

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

int a[55][55],H,W;

void dfs(int i,int j,vector<vector<int>>&used){
	used[i][j] = true;
	rep(k,0,8){
		int ni = i + dy[k],nj = j + dx[k];
		if(ni<0||nj<0||H<=ni||W<=nj)continue;
		if(a[ni][nj]==0)continue;
		if(used[ni][nj])continue;
		dfs(ni,nj,used);
	}
}

signed main()
{
	//IOS();
	vector<int>ans;
	while(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>>a[i][j];
			}
		}
		vector<vector<int>>used(H,vector<int>(W,0));
		int cnt = 0;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(used[i][j])continue;
				if(a[i][j]==0)continue;
				dfs(i,j,used);
				cnt++;
			}
		}
		ans.push_back(cnt);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}