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 "bits/stdc++.h"

using namespace std;

void dfs(int i, int j, const vector<vector<int> >& c, vector<vector<int> >& arrived, int count) {
    arrived[i][j] = count;

    if (c[i + 1][j] == 1 && arrived[i + 1][j] == 0) {
        dfs(i + 1, j, c, arrived, count);
    }
    if (c[i - 1][j] == 1 && arrived[i - 1][j] == 0) {
        dfs(i - 1, j, c, arrived, count);
    }
    if (c[i][j + 1] == 1 && arrived[i][j + 1] == 0) {
        dfs(i, j + 1, c, arrived, count);
    }
    if (c[i][j - 1] == 1 && arrived[i][j - 1] == 0) {
        dfs(i, j - 1, c, arrived, count);
    }
    if (c[i + 1][j + 1] == 1 && arrived[i + 1][j + 1] == 0) {
        dfs(i + 1, j + 1, c, arrived, count);
    }
    if (c[i + 1][j - 1] == 1 && arrived[i + 1][j - 1] == 0) {
        dfs(i + 1, j - 1, c, arrived, count);
    }
    if (c[i - 1][j + 1] == 1 && arrived[i - 1][j + 1] == 0) {
        dfs(i - 1, j + 1, c, arrived, count);
    }
    if (c[i - 1][j - 1] == 1 && arrived[i - 1][j - 1] == 0) {
        dfs(i - 1, j - 1, c, arrived, count);
    }
}

void Main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

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

        vector<vector<int> > arrived(h + 2, vector<int>(w + 2, 0));
        int count = 0;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (c[i][j] == 1 && arrived[i][j] == 0) {
                    ++count;
                    dfs(i, j, c, arrived, count);
                }
            }
        }

        set<int> ans;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (arrived[i][j] > 0) {
                    ans.insert(arrived[i][j]);
                }
            }
        }
        cout << ans.size() << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

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