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 <vector>
#include <queue>
using namespace std;
int main() {
  int w, h, s, t, x, y, ans;
  queue<vector<int>> que;
  while (cin >> w >> h, w) {
    ans = 0;
    vector< vector<int> > m(h+2, vector<int>(w+2, 0));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
	cin >> m[i+1][j+1];
      }
    }
    s = 0; t = 0;
    while (s != h+1 || t != w+1) {
      if (!m[s][t]) {
	if (t < w+1) ++t;
	else {
	  t = 0;
	  ++s;
	}
      } else {
	que.push({s, t});
	m[s][t] = 0;
	while (!que.empty()) {
	  x = que.front()[0];
	  y = que.front()[1];
	  que.pop();
	  if (m[x-1][y]) {
	    m[x-1][y] = 0;
	    que.push({x-1, y});
	  }
	  if (m[x+1][y]) {
	    m[x+1][y] = 0;
	    que.push({x+1, y});
	  }
	  if (m[x][y-1]) {
	    m[x][y-1] = 0;
	    que.push({x, y-1});
	  }
	  if (m[x][y+1]) {
	    m[x][y+1] = 0;
	    que.push({x, y+1});
	  }
	  if (m[x+1][y+1]) {
	    m[x+1][y+1] = 0;
	    que.push({x+1, y+1});
	  }
	  if (m[x+1][y-1]) {
	    m[x+1][y-1] = 0;
	    que.push({x+1, y-1});
	  }
	  if (m[x-1][y+1]) {
	    m[x-1][y+1] = 0;
	    que.push({x-1, y+1});
	  }
	  if (m[x-1][y-1]) {
	    m[x-1][y-1] = 0;
	    que.push({x-1, y-1});
	  }
	}
	++ans;
      }
    }
    cout << ans << endl;
    m.erase(m.begin(), m.end());
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

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

int m[55][55];
int main(){
	while(1){
		int h,w,ret=0;
		cin>>w>>h;
		if(w==0)return 0;
		memset(m, 0, sizeof(m));
		REP(i,w+2){
			m[0][i]=0;
			m[h+1][i]=0;
		}
		REP(i,h+2){
			m[i][0]=0;
			m[i][w+1]=0;
		}
		REP(i,h)REP(j,w)cin>>m[i+1][j+1];
		priority_queue<PII> q;
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(m[i][j]==1){
					ret++;
					q.push(MP(i,j));
					while(!q.empty()){
						PII tmp = q.top();
						q.pop();
						if(m[tmp.first][tmp.second]==0)continue;
						m[tmp.first][tmp.second] = 0;
						REP(i,8)q.push(MP(tmp.first+dy[i], tmp.second+dx[i]));
					}
				}
			}
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include <unordered_map>  
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;

bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;

void checkIsland(int x, int y) {
	if (x < 0 || x >= w || y >= h || y < 0) { return; }
	if (!island[x][y]) return;
	checked[x][y] = true;
	checkIsland(x + 1, y);
	checkIsland(x + 1, y + 1);
	checkIsland(x + 1, y - 1);
	checkIsland(x - 1, y + 1);
	checkIsland(x, y + 1);
}

int main() {
	cin >> w >> h;
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			checked[i][j] = false;
			cin >> island[i][j];
		}
	}
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			if (checked[i][j]) continue;
			if (island[i][j]) {
				count1++;
				checkIsland(i, j);
			}
			else {
				checked[i][j] = true;
			}
		}
	}
	cout << count1 << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int field[64][64];
int cnt;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		cnt = 0;

		scanf("%d %d", &w, &h);
		if (w == 0){
			break;
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (field[i][j]){
					cnt++;
					dfs(j, i);
				}
			}
		}

		printf("%d\n", cnt);
	}

	return (0);
}

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

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (field[ny][nx] && nx >= 0 && ny >= 0 && nx < w && ny < h){
			dfs(nx, ny);
		}
	}
}

C++: #include <iostream>
#define R 1
#define U 0
using namespace std;
int w,h;
int c[60][60];

void rec(int x,int y){
  if( x<0 || x>=h ) return;
  if( y<0 || y>=w ) return;
  if( c[x][y]==U )  return;

  c[x][y]=U;
  for( int i=-1;i<=1;i++ ){
    for( int j=-1;j<=1;j++ ){
      if( i==j && i==0 ) continue;
      rec( x+i,y+j );
    }
  }
}

int main(){
  while( cin>>w>>h && (w|h) ){
    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++ )
	cin >> c[i][j];
    int ans=0;
    for( int i=0;i<h;i++ ){
      for( int j=0;j<w;j++ ){
	if( c[i][j]==R ){
	  ans++;
	  rec( i,j );
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}