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, cnt;
int island[51][51];
int dx[] = {1, 1, 0, -1, -1, -1,  0,  1};
int dy[] = {0, 1, 1,  1,  0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || w <= ny || ny < 0 || h < ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

  while(cin >> w >> h, w || h){

    for(int i = 0 ; i < h ; i++)
      for(int j = 0 ; j < w ; j++)
	cin >> island[i][j];

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

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;

#define INF 999999999

int map[52][52];
bool ch[52][52];
int ans;

void dfs(int map[52][52], bool c[52][52], int x, int y){
	//cout << x << " " << y << " " << ans << endl;
	c[x][y] = 1;
	if(map[x-1][y]==1 && c[x-1][y]==0) dfs(map,c,x-1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x+1][y]==1 && c[x+1][y]==0) dfs(map,c,x+1,y);
	if(map[x+1][y-1]==1 && c[x+1][y-1]==0) dfs(map,c,x+1,y-1);
	if(map[x+1][y+1]==1 && c[x+1][y+1]==0) dfs(map,c,x+1,y+1);
	if(map[x][y-1]==1 && c[x][y-1]==0) dfs(map,c,x,y-1);
	if(map[x][y+1]==1 && c[x][y+1]==0) dfs(map,c,x,y+1);
}

int main(){
	int i, j, w, h;
	while(1){
		cin >> w >> h;
		if(!w) break;
		for(i=0;i<=h+1;i++){
			for(j=0;j<=w+1;j++){
				if(i==0||i==h+1||j==0||j==w+1)map[i][j]=0;
				else cin >> map[i][j];
				ch[i][j] = 0;
			}
		}
		ans = 0;
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				if(map[i][j]&&!ch[i][j]){
					dfs(map,ch,i,j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
	return 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<iostream>
using namespace std;

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

void solve(int x,int y){
	if(m[y][x] == 1){
		m[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i < w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

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

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

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(m[i][j] == 1){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}

C++: #include<iostream>

using namespace std;

int idirs[] = {0, 0, 1, 1, 1, -1, -1, -1};
int jdirs[] = {1, -1, 0, 1, -1, 0, 1, -1};

int map[52][52];

void dfs(int i, int j) {
    if (!map[i][j]) return;
    map[i][j] = 0;
    for (int k = 0; k < 8; ++k) {
        dfs(i + idirs[k], j + jdirs[k]);
    }
}

int main() {
    while (1) {
        int w, h;

        cin >> w >> h;

        if (w == 0) break;

        for (int i = 0; i < 52; ++i)
            for (int j = 1; j < 52; ++j)
                map[i][j] = 0;

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

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

        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>
//#include <bits/stdc++.h>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int w, h, cnt = 0;

void srch(int x, int y, vector<vector<int>> &inp, vector < vector <bool>> &hit)
{
	if (x < 0 || w <= x || y < 0 || h <= y || inp[y][x] == 0) { return; }
	if (hit[y][x]) return;
	hit[y][x] = true;
	cnt += 1;
	srch(x + 1, y, inp, hit);
	srch(x - 1, y, inp, hit);
	srch(x, y + 1, inp, hit);
	srch(x, y - 1, inp, hit);
	srch(x + 1, y + 1, inp, hit);
	srch(x + 1, y - 1, inp, hit);
	srch(x - 1, y + 1, inp, hit);
	srch(x - 1, y - 1, inp, hit);
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> w >> h && w != 0 && h != 0)
	{
		vector<vector<int>> inp(h, vector<int>(w, 0));
		vector<vector<bool>> hit(h, vector<bool>(w, false));
		int ans = 0;
		rep(y, h) rep(x, w){ cin >> inp[y][x]; }
		rep(y, h) rep(x, w)
		{
			srch(y, x, inp, hit);
			if (cnt > 0) { ans += 1; }
			cnt = 0;
		}
		printf("%d\n", ans);
	}

	return 0;
}