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;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = INT_MAX;
const ll LLINF = INT64_MAX;
const double PI = acos(-1);
const int MOD = 1000000007;

//-------------------------------------

using P = pair<int, int>;

void dfs(int x, int y, int h, int w, vector<vector<int>> &g, vector<vector<bool>> &seen)
{
    stack<P> st;
    st.push(P(x, y));
    while (st.size())
    {
        P now = st.top();
        st.pop();
        seen[now.second][now.first] = true;
        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                int nx = now.first + dx;
                int ny = now.second + dy;
                if (0 <= nx && nx < w && 0 <= ny && ny < h && !seen[ny][nx] && g[ny][nx] == 1)
                {
                    st.push(P(nx, ny));
                }
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true)
    {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0)
        {
            return 0;
        }
        // 陸は1、海は0
        vector<vector<int>> g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                cin >> g[i][j];
            }
        }
        vector<vector<bool>> seen(h, vector<bool>(w, false));
        int ans = 0;
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (!seen[y][x] && g[y][x] == 1)
                {
                    dfs(x, y, h, w, g, seen);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;

int c[60][60];
bool visited[60][60];
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int h, w;

bool check(int x, int y){
  if(x < 0 || y < 0)return false;
  if(x >= w || y >= h)return false;
  if(visited[y][x])return false;
  if(c[y][x] == 0)return false;
  return true;
}

void dfs(int x, int y){ 
  if(!check(x, y))return;

  visited[y][x] = true;
  for(int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i]);
  }
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        scanf("%d", &c[i][j]);
      }
    }

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

    int cnt = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!visited[i][j] && c[i][j] == 1){
          dfs(j, i);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>

using namespace std;

#define WH 52

int dx[8]={ 0, 1, 1, 1, 0,-1,-1,-1};
int dy[8]={-1,-1, 0, 1, 1, 1, 0,-1};
int f[WH][WH];

void dfs(int,int);

int main(int argc, char const *argv[])
{
	int w,h;


	int cnt;

	while(1){
		cin>>w>>h;
		if(w+h==0) break;

		for(int i1=0;i1<WH;i1++){
			for(int i2=0;i2<WH;i2++){
				f[i1][i2]=0;
			}
		}

		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				cin>>f[i1][i2];
			}
		}

		cnt=0;
		for(int i1=1;i1<=h;i1++){
			for(int i2=1;i2<=w;i2++){
				if(f[i1][i2]==1){
					f[i1][i2]=0;
					dfs(i2,i1);
					cnt++;
				}
			}
		}

		cout<<cnt<<endl;

	}

	return 0;
}

void dfs(int x,int y){
	for(int i1=0;i1<8;i1++){
		for(int i2=0;i2<8;i2++){
			if(f[y+dy[i1]][x+dx[i2]]==1){
				f[y+dy[i1]][x+dx[i2]]=0;
				dfs(x+dx[i2],y+dy[i1]);
			}
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int draw ( int width, int height,std::vector<std::vector<int>> map){
	for(int i =0; i < height; i ++){
		for(int j = 0; j < width; j ++){
			cout << map[j][i] << " ";
		}
		cout << endl;

	}
	return 0;
}
//未探索陸地か
bool judge (int width,int height,std::vector<std::vector<int>> map, int x, int y){
	if( x >=0 && x  < width && y >= 0 && y < height && map[x][y] == 1){
		return true;
	}
	return false;
}
//周辺を探査して塗りつぶす
std::vector<std::vector<int>> Conqure (int width,int height,std::vector<std::vector<int>> map, int x, int y, int color) {
	map[x][y] = color;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			if(judge(width,height,map,x+dx,y+dy)){
				map[x+dx][y+dy] = color;
				if(!(dx == 0 && dy == 0)){
					map = Conqure(width,height,map,x+dx,y+dy,color);
				}
				
			}
		}
	}
	return map;
}
int main () {
	while(true){
			int w,h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	std::vector<std::vector<int>> map;
		map.resize(w);		// ()内の数字が要素数になる
	for( int i=0; i<w; i++ ){
		map[i].resize(h);
	}
	int landCount = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> map[j][i];
		}
	}
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(judge(w,h,map,j,i)){
				landCount += 1;
				map = Conqure(w,h,map,j,i,landCount + 1);
				//draw(w,h,map);
			}
		}
	}
	cout <<  landCount << endl;
	}

}