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

int w,h;
int mp[50][50];

void rec(int y,int x){
	if(mp[y][x]==0)return;
	mp[y][x]=0;
	if(x+1<w)rec(y,x+1);
	if(y+1<h)rec(y+1,x);
	if(x-1>=0)rec(y,x-1);
	if(y-1>=0)rec(y-1,x);
	if(x+1<w&&y-1>=0)rec(y-1,x+1);
	if(y+1<h &&y+1<h)rec(y+1,x+1);
	if(x-1>=0&&y+1<h)rec(y+1,x-1);
	if(y-1>=0&&x-1>=0)rec(y-1,x-1);
}

int main(){
	int x,y,ret;
	while(cin >> w >> h && w && h){
		ret = 0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> mp[i][j];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(mp[i][j]){
					rec(i,j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>

#define rep(i, n) for(int i = 0 ; i < n ; i++)

using namespace std;

int area[50][50], cnt = 0;
int w, h;

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

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

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

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

    rep(i, h){
      rep(j, w){
	cin >> area[i][j];
      }
    }

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    
    cout << cnt << '\n';
    cnt = 0;
  }
}

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

int main(){
  bool map[52][52];
  int w,h;

  while(cin >> w >> h && w>0 && h>0){
    for(int i=0;i<52;i++){
      for(int j=0;j<52;j++)
        map[i][j]=false;
    }

    int land=0;

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

    int visited=0,cnt=0;
    queue<int> cur;
    int d[8]={-w-3,-w-2,-w-1,-1,1,w+1,w+2,w+3}

    while(visited<land){
      cnt++;
      for(int i=0;i<(w+2)*(h+2);i++){
        if(map[i/(w+2)][i%(w+2)]){
          map[i/(w+2)][i%(w+2)]=false;
          visited++;
          cur.push(i);
          break;
        }
      }
      while(!cur.empty()){
        for(int i=0;i<8;i++){
          if(map[(cur.front()+d[i])/(w+2)][(cur.front()+d[i])%(w+2)]){
            map[(cur.front()+d[i])/(w+2)][(cur.front()+d[i])%(w+2)]=false;
            visited++;
            cur.push(cur.front()+d[i]);
          }
        }
        cur.pop();
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dx[8] = {0,1,0,-1,1,1,-1,-1};

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

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>mas[i][j];
            }
        }
        cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j]==1){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repr(i,a,n) for(int i=a;i>=n;i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int w,h;

int dfs(int i, int j, int c[][51]) {
    if(c[i][j] == 1) {
        c[i][j] = 0;
        rep(di,i-1,i+2) {
            rep(dj,j-1,j+2) {
                if(di != i || dj != j) {
                    if(di<0 || di>=h || dj<0 || dj>=w) continue;
                    if(c[di][dj] == 1) dfs(di,dj,c);
                }
            }
        }
        return 1;
    }
    return 0;
}

int main() {
    int c[51][51];

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

        rep(i,0,h) {
            rep(j,0,w) {
                cin >> c[i][j];
            }
        }

        int ret = 0;
        rep(i,0,h) {
            rep(j,0,w) {
                ret += dfs(i,j,c);
            }
        }

        cout << ret << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

vector<pair<ll, ll>> d = {
  {-1, -1},
  {+1, -1},
  {-1, +1},
  {+1, +1},
  {+0, +1},
  {+0, -1},
  {+1, +0},
  {-1, +0},
};

void dfs(ll x, ll y, vector<vector<ll>> &graph) {
  graph[y][x] = 0;
  for(auto v: d) {
    ll nx = v.first + x;
    ll ny = v.second + y;
    if (0 <= ny && ny < graph.size() && 0 <= nx && nx < graph[y].size() && graph[ny][nx]) {
      dfs(nx, ny, graph);
    }
  }
}

bool solve() {
  ll input(w, h);
  if (w == 0 && h == 0) return false;

  vector<vector<ll>> graph(h);
  REP(i, h) {
    vector<ll> r(w);
    REP(j, w) cin >> r[j];
    graph[i] = r;
  }

  ll cnt = 0;
  REP(i, h) REP(j, w) {
    if (graph[i][j]) {
      dfs(j, i, graph);
      cnt++;
    }
  }
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve());
}