Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <cstdio>

int w, h;
int data[64][64];
int movex[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int movey[] = {-1, 0, 1, -1, 1, -1, 0, 1};

void dfs(int x, int y);

int main(void)
{	
	while (scanf("%d %d", &w, &h)){
		if (w == 0 && h == 0) break;
		
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				data[i][j] = 0;
			}
		}
		
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf("%d", &data[i][j]);
			}
		}
		
		int res = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (data[i][j] == 1){
					dfs(i, j);
					res++;
				}
			}
		}
		
		printf("%d\n", res);
	}
	
	return (0);
}

void dfs(int x, int y)
{
	data[x][y] = 0;
	
	for (int i = 0; i < 8; i++){
		if (data[x + movex[i]][y + movey[i]] == 1){
			dfs(x + movex[i], y + movey[i]);
		}
	}
	
	return;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//int cnt = 0;

void depth_search(int ,int,vector< vector<int> > &);
void show(vector<vector <int> > &umi){
  return ;
  cout << "---------------------------" << endl;
  for(int i = 0;i < umi[0].size();i++){
    for(int j = 0;j < umi.size();j++){
      cout << umi[j][i];
    }
    cout << endl;
  }
    cout << "---------------------------" << endl;
  
}
int main(){
  int w,h;
  while(true){
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0)return 0;
    vector< vector<int> > umi(w,vector<int>(h));
    //    cout << "aaaaaaaaaa" << endl;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	cin >> umi[j][i];

      }
    } 

    int cnt = 0;
    for(int i = 0;i < w;i++){
      for(int j = 0;j < h;j++){
	if( umi[i][j] == 1){
	  cnt++;
	  umi[i][j] = 3;
	  depth_search(i,j,umi);
	  show(umi);

	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void depth_search(int x,int y,vector< vector<int> > &umi){
  int dx[3] = {0,1,-1};
  int dy[3] = {0,1,-1};
  for(int i = 0;i < 3;i++){
    for(int j = 0;j < 3;j++){
      if(!(x+dx[i] == -1
	 ||x+dx[i] == umi.size()
	 ||y+dy[j] == -1
	 ||y+dy[j] == umi[0].size())
	 && (umi[x+dx[i]][y+dy[j]] == 1)
	 && !(dx[i] == 0 && dy[j] == 0)){
	umi[x+dx[i]][y+dy[j]] = 3;
	depth_search(x+dx[i],y+dy[j],umi);
	
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> P;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int w, h;
	while(cin >> w >> h, w|h){
		vector<vi> v(h, vi(w, 0));
		rep(i, h){
			rep(j, w){
				cin >> v[i][j];
			}
		}

		int ans = 0;
		rep(i, h){
			rep(j, w){
				if(v[i][j] != 1)continue;
				ans++;
				queue<P> q;
				q.push(MP(j,i));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					if(v[p.Y][p.X] != 1) continue;
					v[p.Y][p.X] = 0;
					rep(k, 8){
						int x = p.X+dx[k];
						int y = p.Y+dy[k];
						if(x < 0 || x >= w || y < 0 || y >= h) continue;
						q.push(MP(x, y));
					}
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

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

int h, w;
vector< vector<int> > g;
//vector<vector<bool>> v;

void dfs(int y, int x) {
  g[y][x] = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int ny = y + i;
      int nx = x + j;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
      if (g[ny][nx] == 0) continue;
      dfs(ny, nx);
    }
  }
}

int main() {
  while (cin >> w >> h) {
    if (h == 0 || w == 0) break;
    g.assign(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) {
      cin >> g[i][j];
    }
    //v.assign(h, vector<bool>(w, false));
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] == 0) continue;
      dfs(i, j);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

using namespace std;

const int ddx[8] = {1,1,1,0,0,-1,-1,-1};
const int ddy[8] = {1,0,-1,1,-1,1,0,-1};

bool visited[60][60] ={false};
int v[60][60] = {0};
int h,w;

int dfs(int i ,int j){
    if(visited[i][j]) return 0;
    
    visited[i][j] = true;

    for(int x = 0; x < 8; x++){
        for(int y = 0; y < 8;y++){
            int posx = j + ddx[x];
            int posy = i + ddy[y];
            if(posx >= 0 && posx < w && posy >= 0 && posy < h){
                if(!visited[posy][posx] && v[posy][posx]){
                    dfs(posy, posx);
                }
            }
        }
    }
    return 1;
}

int main()
{
    while(cin >> w >> h, w and h){
        int ans = 0;

        for(int i = 0; i < 60 ;i++){
            for(int j = 0;j < 60; j++){
                visited[i][j] = false;
            }
        }

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

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                if(v[i][i] || !visited[i][j]) ans += dfs(i,j);
            }
        }
        cout << ans << endl;
    }
    return 0;
}