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 <algorithm>

typedef long long ll;
using namespace std;

int H, W;
vector<vector<int> > C;
vector<vector<int> > visited;
int ans = 0;





void dfs(int &i, int &j){
    visited[i][j] = 1;
    for(int y=-1; y<=1; y++){
        for(int x=-1; x<=1; x++){
            int ny = i+y;
            int nx = j+x;
            
            if(ny>=0 && ny<H && nx>=0 && nx<W && C[ny][nx] && !visited[ny][nx]) dfs(ny, nx);
        }
    }
    return;
}




int main() {
    while(1){
        ans = 0;
        visited.clear();
        
        cin >> W >> H; if((W&H)==0) break;
        C.resize(H);
        for(vector<int> &v:C) v.resize(W);
        for(vector<int> &row:C)
            for(int &c:row) cin >> c;

        visited.resize(H);
        for(vector<int> &v:visited) v.resize(W);

        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(C[i][j] && !visited[i][j]){
                    dfs(i,j);
                    ans ++;
                }
            }
        }
        
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

#define int long long

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

void DFS(int x, int y, int fld[50][50])
{
	if (fld[y][x] == 0) return;
	fld[y][x] = 0;
	for (int i = 0; i < 8; i++)
	{
		int nx = x + dx[i], ny = y + dy[i];
		if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue;
		DFS(nx, ny, fld);
	}
}

signed main()
{
	while (cin >> W >> H, W || H)
	{
		int fld[50][50];
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> fld[i][j];
			}
		}

		int cnt = 0;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (fld[i][j] == 1) cnt++;
				DFS(j, i, fld);
			}
		}
		printf("%lld\n", cnt);
	}
}

C++: #include <iostream>
 
using namespace std;
int field[50][50];
int w, h, ans;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0,  0, 1, 1, 1};
 
void check(int x, int y)
{
    if (field[x][y])
        field[x][y] = 0;
    else
        return;
         
    for (int i=0; i<8; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0<= nx && nx<w && 0<=ny&&ny<h) {
            check(nx, ny);
        }
    }
}
 
int main()
{
    while (cin>>w>>h, w||h) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                int i; cin >> i;
                field[x][y] = i;
                }
           }
        
       ans = 0;
       for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
               if (field[x][y]) {
                       ans++;
                       check(x, y);
               }  
            }   
        }
 
        cout << ans << endl;
   }           
     
    return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

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 vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

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

		if(seamap[x-1][y-1]) {
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 0, 52) {
			FOR(j, 0, 52) {
				seamap[i][j] = 0;
			}
		}

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				cin >> seamap[i][j];
			}
		}

		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}

	return 0;
}