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

}