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 <bits/stdc++.h> 

#define int long long
#define double long double

#define REP(i, b) for(int i = 0; i < (b); i++)
#define REPS(i, b) for(int i = 1; i <= (b); i++)
#define ALL(v) (v).begin(), (v).end()

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

using pi = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vvi = vector<vi>;
using vvb = vector<vb>;

const int INF = 1e16;
const int MOD = 1e9+7;

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 w, h;
int d[] = {-1, 0, 1};

void dfs(vvi &c, int y, int x)
{
    c[y][x] = 0;
    REP(i, 3)
    {
        REP(j, 3)
        {
            int ny = y + d[i];
            int nx = x + d[j];
            if(0 <= ny && ny < h && 0 <= nx && nx < w && c[ny][nx] == 1) dfs(c, ny, nx);
        }
    }
}

signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while(true)
    {
        int ans = 0;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vvi c(h, vi(w));
        REP(i, h) REP(j, w) cin >> c[i][j];
        REP(i, h)
        {
            REP(j, w)
            {
                if(c[i][j] == 1)
                {
                    ans++;
                    dfs(c, i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: /************************************
 * ???????????¢?´¢??§??¶?????°?????°??????????????°?????? *
 ************************************/

#include <iostream>
#include <queue>
#include <map>

#define LAND '1'  //??? char???
#define SEA  '0'   //??? char???

using namespace std;

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

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

bool input(vector< vector<char> > &m);
void delete_island(vector< vector< char > > &m, int x, int y);
void output(vector< vector< char> > m);


int main(){
  int w, h;
    
  while(cin >> w >> h, w && h){
    vector< vector< char > > m(h+2, vector<char>(w+2));
    input(m);
    
    int island = 0;
    for(int y = 0; y < m.size(); y++){
      for(int x = 0; x < m[y].size(); x++){
	if(m[y][x] == LAND){
	  delete_island(m,x,y);
	  island++;
	}
      }
    }
    cout << island << endl;
  }
}


bool input(vector< vector<char> > &m){
  for(int y = 0; y < m.size(); y++){
    for(int x = 0; x < m[y].size(); x++){
      if(y == 0 || x == 0 ||
	 y == m.size()-1 || x == m[y].size()-1){
	m[y][x] = SEA;
      }else{
	if(!(cin >> m[y][x])) return false;
      }
    }
  }
  return true;
}


 void delete_island(vector< vector< char > > &m, int x, int y){
   queue< pair< int, int > > q;
   q.push(make_pair(x,y));

   while(!(q.empty())){
     pair<int, int> cur = q.front();
     q.pop();
     if(m[cur.second][cur.first] == LAND){
       m[cur.second][cur.first] = SEA;
       for(int i = 0; i < sizeof(dx)/sizeof(int); i++){
	 q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
       }
     }
   }
}

void output(vector< vector< char> > m){
  for(auto y : m){
    for(auto x: y){
      cout << x << " ";
    }
    cout << endl;
  }
  cout << endl;
}

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

int main() {
  int w, h;
  while(cin >> w >> h, w || h) {
    set<pair<int, int> > blacks;
    for(int y = 0; y < h; ++y) for(int x = 0; x < w; ++x) {
      int d;
      cin >> d;
      if(d) blacks.insert(make_pair(y, x));
    }
    function<void(int, int)> countIslands = [&](int y, int x) {
      if(blacks.count(make_pair(y, x))) {
        blacks.erase(make_pair(y, x));
        int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
        int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
        for(int i = 0; i < 8; ++i) countIslands(y + dy[i], x + dx[i]);
      }
    };
    int ans = 0;
    while(!blacks.empty()) {
      countIslands((*begin(blacks)).first, (*begin(blacks)).second);
      ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[52][52];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i,k,n) for(int i = k; i < (int)(n); i++)
using namespace std;

int w,h;
int field[51][51];

void dfs(int y, int x){
        field[y][x]= 0;

        int dx[] = {-1,-1,-1,0,0,1,1,1};
        int dy[] = {-1,0,1,-1,1,-1,0,1,};
        rep(i,8){
                int nx = x+dx[i];
                int ny = y+dy[i];
                if(nx>=0 && nx<w && ny>=0 && ny<h && field[ny][nx] == 1) dfs(ny,nx);
        }
        return;
}

int main(){
        while(true) {
                cin >> w >> h;
                if(w==0) break;
                rep(y,h) rep(x,w){
                        cin >> field[y][x];
                }
                int ans = 0;
                rep(y,h) rep(x,w){
                        if(field[y][x] == 1) {
                                dfs(y,x);
                                ans++;
                        }
                }
                cout << ans << endl;
        }

        return 0;
}