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

using namespace std;

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

int w, h;
int island[100][100];

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

  repi(mat_y, -1, 2) {
    repi(mat_x, -1, 2) {
      if (x + mat_x < 0 || y + mat_y < 0 || x + mat_x >= w || y + mat_y >= h) continue;
      if (island[y + mat_y][x + mat_x]) dfs(x + mat_x, y + mat_y);
    }
  }
}

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

    rep(y, h) {
      rep(x, w) {
        cin >> island[y][x];
      }
    }
    int cnt = 0;
    rep(y, h) {
      rep(x, w) {
        if (island[y][x]) {
          ++cnt;
          dfs(x, y);
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

// How Many Islands?(1160)
const int dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void solve(vector<vector<int> >&f, int x, int y)
{
	if (x < 0 || x >= (int)f[0].size() || y < 0 || y >= (int)f.size() || f[y][x] == 0)
		return;

	f[y][x] = 0;
	for (int i = 0; i < 8; ++i)
		solve(f, x+dx[i], y+dy[i]);
	return;
}

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

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

		int ans = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (f[i][j]) {
					solve(f, j, i);
					++ans;
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

C++: #include <cstdio>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

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

void dfs( int i, int j )
{
  c[i][j] = 0;

  for( int dx = -1; dx <= 1; ++dx ) for( int dy = -1; dy <= 1; ++dy )
  {
    int ni = i+dy, nj = j+dx;

    if( ni >= 0 && ni < h && nj >= 0 && nj < w && c[ni][nj] == 1 )
      dfs( ni, nj );
  }

  return;
}

int main()
{
  while( scanf( "%d%d", &w, &h ), w|h )
  {
    rep( i, h ) rep( j, w )
      scanf( "%d", &c[i][j] );

    int cnt = 0;
    rep( i, h ) rep( j, w ) if( c[i][j] == 1 )
      dfs( i, j ), ++cnt;    

    printf( "%d\n", cnt );
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

int dx[]={1,1,0,-1,-1,-1,0,1}, dy[]={0,-1,-1,-1,0,1,1,1};
int cnt = 0;
int gg[60][60];
int w,h;
void solve(int x, int y){
    //cout<<x<<" "<<y<<"aaaaaa"<<endl;
    gg[x][y]=0;
    //if(x==2 && y == 48) cout<<"probrem"<<endl;
    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        /*if(x==2 && y==48){
            cout<<nx<<" "<<ny<<endl;
        }*/
        if(nx<h && nx>=0 && ny<w && ny>=0 && gg[nx][ny]==1){
            //cout<<nx<<" "<<ny<<endl;
            solve(nx,ny);
        }
    }
}

int main(){
    while(1){
        cin>>w>>h;
        rep(i,60) rep(j,60) gg[i][j]=0;
        cnt = 0;
        if(w==0 && h==0) break;
        rep(i,h) rep(j,w) cin>>gg[i][j];
        rep(i,h) rep(j,w){
            if(gg[i][j]==1) {
                //cout<<i<<" "<<j<<"start"<<endl;
                cnt++;
                solve(i,j);
                /*rep(i,h){
                    rep(j,w) cout<<gg[i][j]<<" ";
                    cout<<endl;
                }*/
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx > w || ny > h)
            continue;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

        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) {
                    isl++;
                    dfs(i, j);
                }
            
        cout << isl << endl;
    }

    return 0;
}