Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <cstdio>
#include <stack>
#include <utility>

using namespace std;

typedef pair<int, int> point;

int w, h;
int c[50][50];
const int d[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int count()
{
    stack<point> s;
    bool v[50][50] = {};
    int n = 0;

    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (v[x][y] || c[x][y] == 0)
                continue;

            s.push(point(x, y));
            v[x][y] = true;
            ++n;
            while (!s.empty()) {
                int x = s.top().first, y = s.top().second;
                s.pop();

                for (int i = 0; i < 8; ++i) {
                    int xx = x + d[i][0], yy = y + d[i][1];
                    if (0 <= xx && xx < w && 0 <= yy && yy < h
                            && !v[xx][yy] && c[xx][yy] == 1) {
                        s.push(point(xx, yy));
                        v[xx][yy] = true;
                    }
                }
            }
        }
    }

    return n;
}

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

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

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

    return 0;
}

C++: #include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;


//AOJ How many islands?
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int dx[8] = {1,-1,0,0,1,1,-1,-1};
int dy[8] = {0,0,1,-1,1,-1,1,-1};
int h,w;
using Graph = vector<vector<int>>;
vector<vector<bool>> seen;
vector<vector<int>> maze;

void dfs(P p){
    seen[p.first][p.second] = true;

    for(int i = 0;i < 8;i++){
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
        if(seen[ny][nx]) continue;
        if(maze[ny][nx] == 0) continue;

        dfs(make_pair(ny,nx));
    }
}

void recur(int w,int h) {
    int count = 0;
    maze.assign(h,vector<int>(w,0));
    seen.assign(h,vector<bool>(w,false));
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            cin >> maze[i][j];
        }
    }

    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(maze[i][j]==0) continue;
            if(seen[i][j]) continue;
            dfs(make_pair(i,j));
            count++;
        }
    }
    
    cout << count << endl;
    w = 0;
    h = 0;
}

int main(){
    while(true){
        cin >> w >> h;
        if(w*w + h*h == 0) break;
        recur(w,h);
    }
    return 0;
}



// #include <bits/stdc++.h>
// #include <string>
// #include <vector>
// #define rep(i,n) for (int i = 0;i < (n); ++i)
// using namespace std;
// using ll = long long;
// using P = pair<int,int>;


// //AOJ How many islands?
// //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

// int dx[8] = {1,-1,0,0,1,1,-1,-1};
// int dy[8] = {0,0,1,-1,1,-1,1,-1};
// int h,w;
// using Graph = vector<vector<int>>;
// vector<vector<bool>> seen;
// vector<vector<int>> maze;

// void dfs(P p){
//     seen[p.first][p.second] = true;

//     for(int i = 0;i < 8;i++){
//         int nx = p.second + dx[i];
//         int ny = p.first + dy[i];
//         if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
//         if(seen[ny][nx]) continue;
//         if(maze[ny][nx] == 0) continue;

//         dfs(make_pair(ny,nx));
//     }
// }

// int main() {
//     cin >> w >> h;
//     int count = 0;
//     maze.assign(h,vector<int>(w,0));
//     seen.assign(h,vector<bool>(w,false));
//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             cin >> maze[i][j];
//         }
//     }

//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             if(maze[i][j]==0) continue;
//             if(seen[i][j]) continue;
//             dfs(make_pair(i,j));
//             count++;
//         }
//     }
    
//     cout << count << endl;
// }

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};

int w,h,ans=0;
bool c[55][55];

void bfs(int y,int x)
{
    c[y][x]=0;
    REP(i,8) 
    {
        int ny=y+dy[i];int nx=x+dx[i];
        if(c[ny][nx]&&ny>=0&&ny<h&&nx>=0&&nx<w)bfs(ny,nx);
    }
}

int main()
{

    while(cin>>w>>h,w>0)
    {
        REP(i,h)REP(j,w)cin>>c[i][j];
        ans=0;c[55][55]={};
        REP(i,h)REP(j,w)if(c[i][j]){bfs(i,j);ans++;}
        cout<<ans<<endl;
    }
}