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 <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

ll gcd(ll a, ll b) {
  ll tmp;
  if ( b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b !=0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

ll lcm(ll a, ll b) {
  return a * b / gcd(a, b);
}

int d[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int w, h;
void dfs(int y, int x, vector<vector<int>> &v) {
  v[y][x] = 0;
  for(int i = 0; i<8; i++) {
    int ny = y + d[i][0];
    int nx = x + d[i][1];
    if(0<=ny and ny<h and 0<=nx and nx<w and v[ny][nx] == 1) {
      dfs(ny, nx, v);
    }
  }
}

int main(void) {
  while(1) {
    cin>>w>>h;
    if(w == 0 and h == 0)
      break;
    vector<vector<int>> v;
    for(int i = 0; i<h; i++) {
      vector<int> vv;
      for(int j=0; j<w; j++) {
	int x;
	cin>>x;
	vv.push_back(x);
      }
      v.push_back(vv);
    }
    int cnt = 0;
    for(int i=0; i<h; i++) {
      for(int j=0; j<w; j++) {
	if (v[i][j] == 1) {
	  cnt++;
	  dfs(i, j, v);
	}

      }
    }
    std::cout << cnt << std::endl;
  }
  return 0;
}

C++: // https://onlinejudge.u-aizu.ac.jp/challenges/search/titles/1160
// dfs

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>

using namespace std;

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

typedef long long ll;

int field[55][55];
int W, H;

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

void Main()
{
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        memset(field, 0, sizeof(field));
        rep(i, H)
        {
            rep(j, W)
            {
                cin >> field[i][j];
            }
        }
        int cnt = 0;
        rep(i, H)
        {
            rep(j, W)
            {
                if (field[i][j] == 1) {
                    dfs(i, j);
                    ++cnt;
                }
            }
        }
        cout << cnt << endl;
    }
}

int main()
{
    // cin.tie(nullptr);
	// ios_base::sync_with_stdio(false);
	Main();
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> c;
long long dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long dy[8] = {-1, 0, 1, 1, -1, -1, 0, 1};
long long W, H;

void DFS(long long y, long long x)
{
    c[y][x] = 0;
    for (long long i = 0; i < 8; i++)
    {
        long long tx = x + dx[i];
        long long ty = y + dy[i];

        if (tx < 0 || ty < 0 || tx >= W || ty >= H)
            continue;

        if (c[ty][tx] == 0) //if(tx~)と逆だとover
            continue;

        DFS(ty, tx);
    }
}

int main()
{

    while (cin >> W >> H)
    {
        if (H == 0 && W == 0)
            break;
        long long cnt = 0;
        c.assign(H, vector<long long>(W, 0));
        //bool seen[H][W] = {false};

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                cin >> c[i][j];
            }
        }

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                //if (c[i][j] == 1) この条件だと,海の近くに島があるとそれを塗りつぶしてしまう。
                //    cnt++;
                if (c[i][j] == 0) //この条件を入れないと,海隣の島を塗りつぶしてしまう。
                    continue;
                DFS(i, j);
                cnt++;
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

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

using namespace std;

#define llong long long 
int w,h;
int sum;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int H, int W){
	if(area[H][W] == 0)return;
	area[H][W] = 0;
	for(int i = 0; i < 8; i++){
		int sh = H + nh[i];
		int sw = W + nw[i];
		if(sh < 0 || sw < 0 || sh >= h || sw >= w) continue;
		solve(sh, sw);
	}

	return;
}

int main(){

	while(cin >> w >> h, w){
		sum = 0;
		fill_n(*area, 50 * 50, 0);
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}	

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

		cout << sum << endl;
	}
	return 0;
}