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;

#define rep(i, n) REP(i, 0, n)
#define REP(i,k,n) for(int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

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

template<class T = ll> T in() {
    T _in;
    cin >> _in;
    return _in;
}

int w, h;
bool c[55][55] = {};

void DFS(int y, int x) {
    c[y][x] = 0;
    rep(i, 8) {
        if(y+dy[i]<0 || y+dy[i]>=h || x+dx[i]<0 || x+dx[i]>=w) continue;
        if(c[y + dy[i]][x + dx[i]]) {
            DFS(y + dy[i], x + dx[i]);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

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

        int ans = 0;
        // 探索
        rep(y, h) {
            rep(x, w) {
                if (c[y][x]) {
                    DFS(y, x);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    };
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;


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

void dfs(int px , int py){
	int tx , ty;
	land[py][px]=0;
	for(int i=-1 ; i<=1 ; ++i){
		for(int j=-1 ; j<=1 ; ++j){
			tx=px+i , ty=py+j;
			if(tx<0 || tx>=w || ty<0 || ty>=h) continue;
			if(land[ty][tx]) dfs(tx , ty);
		}
	}
}


int main(){
	int cnt=0;
	while(cin >> w >> h , w||h){
		for(int i=0 ; i< h ; ++i){
			for(int j=0 ; j< w ; ++j){
				cin >> land[i][j];
			}
		}
		for(int i=0 ; i<h ; ++i){
			for(int j=0 ; j<w ; ++j){
				if(land[i][j]){
					dfs( j , i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt=0;
	}
	return 0; 
}

C++: #include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;


vector<string>road;
int ix[] = { 0,1,1,1,0,-1,-1,-1 };
int iy[] = { -1,-1,0,1,1,1,0,-1 };
void dfs(int y, int x, int h, int w) {
	road[y][x] = '0';
	rep(i, 8) {
		int dx = x + ix[i], dy = y + iy[i];
		if (dx < 0 || w <= dx || dy < 0 || h <= dy || road[dy][dx] == '0')continue;
		dfs(dy, dx, h, w);
	}
}

int main() {
	while (1) {
		int h, w, cnt = 0;
		string k;
		cin >> w >> h;
		if (h == 0 || w == 0)break;
		road.resize(h);
		road.assign(h, "");
		rep(i, h)rep(j, w) {
			cin >> k;
			road[i] += k;
		}
		rep(i, h)rep(j, w) {
			if (road[i][j] == '0')continue;
			dfs(i, j, h, w);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <math.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void dfs(int x, int y, vector<vector<int>>& field) {
  if (0 <= x && x < field.size() && 0 <= y && y < field[0].size()) {
    if (field[x][y] == 0) {
      return;
    }
  } else {
    return;
  }

  field[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      dfs(x + dx, y + dy, field);
    }
  }
  return;
}

int main() {
  vector<int> ans_v;
  for (int k = 0; k < 100; k++) {
    int w, h;
    cin >> w >> h;
    if (w == 0) {
      break;
    }

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

    ans_v.push_back(ans);
  }

  for (int i = 0; i < ans_v.size(); i++) {
    cout << ans_v[i] << endl;
  }

  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <bitset>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<bool> VB;

int h, w;
VVI c(55, VI(55));
vector<VB> visited(55, VB(55));

int d1[] = { 1,1,1,0,-1,-1,-1,0 };
int d2[] = { -1,0,1,1,1,0,-1,-1 };

void dfs(int x, int y) {
	if (x < 0 || y < 0 || x >= h || y >= w || c[x][y] == 0 || visited[x][y])return;
	visited[x][y] = true;
	rep(i, 8)dfs(x + d1[i], y + d2[i]);
}


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

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



		int cnt = 0;
		rep(i, h)rep(j, w) {
			//printf("visited[%d][%d]=%s", i, j, visited[i][j] ? "true" : "false");
			if (c[i][j] == 1 && visited[i][j] == false) {
				//puts(" -> o");
				dfs(i, j);
				cnt++;
			}
			else {
				//puts(" -> x");
			}
		}
		cout << cnt << endl;
	}
}