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<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<cstdlib>


#define rep(n,cnt) for(int cnt=0;cnt<n;++cnt)

using namespace std;

template <typename TYPE>
void printArray(TYPE* array, int size){
  rep(size,i) cout << array[i] << "\t" << flush;
  cout << endl;
}

int StringToInt(string str){
  return atoi(str.c_str());
}

#define MAX_W 50
#define MAX_H 50

class State{
public:
  int y;
  int x;
  int island_count;
};

int w,h,c[MAX_W][MAX_H];
int count = 0;

void dfs(int sy, int sx){
  c[sy][sx] = 0;

  for(int vy=-1;vy<=1;++vy){
    for(int vx=-1;vx<=1;++vx){
      int y = sy + vy;
      int x = sx + vx;
      if(c[y][x] == 1) dfs(y,x);
    }
  }
}

int main(){
  while(true){
    count = 0;
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0) break;
    rep(h,y){
      rep(w,x){
	cin >> c[y][x];
      }
    }
    
    rep(h,y){
      rep(w,x){
	if(c[y][x] == 1){
	  dfs(y,x);
	  count++;
	}
      }
    }
    cout << count << endl;
  }
}

C++: #include <iostream>
#include <cstring>

using namespace std;


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



void dfs(int cx, int cy) {
	map[cy][cx] = 0;
	
	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			if (map[cy + dy][cx + dx] == 1) dfs(cx + dx, cy + dy);
		}
	}
}

int solver() {
	int cnt = 0;
	
	for (int i=0; i<H; i++) {
		for (int j=0; j<W; j++) {
			if (map[i][j] == 1) {
				dfs(j, i);
				cnt++;
			}
		}
	}
	
	return cnt;
}

int main() {
	
	while (1) {
		cin >> W >> H; if (W==0 && H==0) break;
		
		memset(map, 0, sizeof(map));
		for (int i=0; i<H; i++)
			for (int j=0; j<W; j++) cin >> map[i][j];
		
		cout << solver() << endl;
	}
	
	return 0;
}

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;
}