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;

int w, h;
int c[50][50];

void dfs(int x, int y) {
  c[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1)
        dfs(nx, ny);
    }
  }
  return;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> c[i][j];
    int islands = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (c[i][j] == 1) {
          islands++;
          dfs(i, j);
        }
      }
    }
    cout << islands << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<utility>
#include<vector>
#include<queue>


#define rep(i, a, n)  for(int i=a; i<n; i++)
#define per(i, a, n)  for(int i=n-1; i>=a; i--)
#define fill0(n)  setfill('0') << right << setw(n)
#define all(x)  (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MAX 1000001
using namespace std;
typedef long long ll;

int w,h;
int c[51][51];

void dfs(int y,int x){
  if(y<1||51<=y||x<1||51<=x||c[y][x]==0)return;
  c[y][x]=0;
  dfs(y-1, x-1);
  dfs(y-1, x);
  dfs(y-1, x+1);
  dfs(y, x-1);
  dfs(y, x+1);
  dfs(y+1, x-1);
  dfs(y+1, x);
  dfs(y+1, x+1);
}

int main(){
  while(true){
    std::cin >> w>>h;
    if(w==0&&h==0)break;
    rep(i,1,h+1){
      rep(j,1,w+1){
        std::cin >> c[i][j];
      }
    }
    int res=0;
    rep(i,1,h+1){
      rep(j,1,w+1){
        // std::cout << c[1][1] << '\n';
        if(c[i][j]==1){
          // std::cout << "/* message */" << '\n';
          dfs(i,j);
          res++;
        }
      }
    }
    std::cout << res << '\n';
  }
  return 0;
}

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

using namespace std;

int w,h;
bool field[50][50];
vector<int> ans;

void dfs(int i,int j);

int main(){
	while(1){
		for(int i = 0;i < 50;i++){
			for(int j = 0;j < 50;j++){
				field[i][j] = false;
			}
		}
		cin >> w >> h;
		if(w + h == 0) break;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				int f;
				cin >> f;
				if(f == 1) field[i][j] = true;
			}
		}
		int count = 0;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(field[i][j]){dfs(i,j);count++;}
			}
		}
		ans.push_back(count);
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	return 0;
}

void dfs(int i,int j){
	field[i][j] = false;
	if(i != 0 	&& j != 0 	&& field[i-1][j-1])dfs(i-1,j-1);
	if(i != 0 				&& field[i-1][j])dfs(i-1,j);
	if(i != 0 	&& j != w-1 && field[i-1][j+1])dfs(i-1,j+1);
	if(			   j != w-1 && field[i][j+1])dfs(i,j+1);
	if(i != h-1 && j != w-1 && field[i+1][j+1])dfs(i+1,j+1);
	if(i != h-1 			&& field[i+1][j])dfs(i+1,j);
	if(i != h-1 && j != 0 	&& field[i+1][j-1])dfs(i+1,j-1);
	if(			   j != 0 	&& field[i][j-1])dfs(i,j-1);
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int mas[100][100];
int dx[8]={1,1,0,-1,-1,-1,0,1},dy[8]={0,1,1,1,0,-1,-1,-1};
int sx,sy,gx,gy,w,h,cnt=0;
void dfs(int x,int y);
int main(){
  while(1){
    cnt=0;
  cin >> w >> h;
  if(w==0 && h==0) break;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mas[i][j];
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mas[i][j]==1){
        cnt++;
        dfs(j,i);
      }
    }
  }
  cout << cnt << endl;
  }
}
void dfs(int x,int y){
  mas[y][x]=0;
  for(int i=0;i<8;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx >= 0&& nx < w && ny >= 0 && ny < h && mas[ny][nx]==1){
        dfs(nx,ny);
      }
  }
}

C++: #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
static const ll maxLL = (ll)1 << 62;

//dfs(bool型)
int dy[]={-1,0,1,0,1,-1,-1,1};
int dx[]={0,1,0,-1,1,1,-1,-1};
bool visited[1000][1000];
int t[1000][1000];
char copy_t[1000][1000];
int h,w;
int si,sj;

void dfs(int y,int x){
  if(y<=0||x<=0)return;
  if(y>h||x>w)return;
  if(t[y][x]==0)return;
  if(visited[y][x])return;
  visited[y][x]=true;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    dfs(ny,nx);
  }
  return;
}

int main(){
    int cnt=0;
    cin>>w>>h;
    repe(i,1,h){
        for(int j=1;j<=w;j++){
            cin>>t[i][j];
        }
    }
    repe(i,1,h){
        repe(j,1,w){
             if(t[i][j]==1 && visited[i][j]==false)cnt++;
        dfs(i,j);
        }
    }
    cout<<cnt<<endl;
    return 0;
}

C++: #include<cstdio>
using namespace std;
void func(int,int);
int a,b;
int sima[51][51];
int cnt;
int y,x;
int main(void)
{
	int i,j;
	scanf("%d",&a);
	scanf("%d",&b);
	while(!(a==0 && b==0)){
		
		for(i=0;i<b;i++){
			for(j=0;j<a;j++){
				scanf("%d",&sima[i][j]);
			}
		}
		cnt=0;
		for(i=0;i<b;i++){
			for(j=0;j<a;j++){
				if(sima[i][j]==1){
					func(i,j);
					cnt++;
				}
			}
		}
		printf("%d\n",cnt);
		scanf("%d",&a);
		scanf("%d",&b);
	}
	return 0;
}
void func(int y,int x)
{
	if(y-1>=0){
		if(sima[y-1][x]==1){
			sima[y-1][x]=0;
			func(y-1,x);
		}
	}
	if(x-1>=0){
		if(sima[y][x-1]==1){
			sima[y][x-1]=0;
			func(y,x-1);	
		}
	}
	if(y+1<=a){
		if(sima[y+1][x]==1){
			sima[y+1][x]=0;
			func(y+1,x);
		}
	}
	if(x+1<=b){
		if(sima[y][x+1]==1){
			sima[y][x+1]=0;
			func(y,x+1);
		}
	}
	if(y-1>=0 && x+1<=b){
		if(sima[y-1][x+1]==1){
			sima[y-1][x+1]=0;
			func(y-1,x+1);
		}
	}
	if(y-1>=0 && x-1>=0){
		if(sima[y-1][x-1]==1){
			sima[y-1][x-1]=0;
			func(y-1,x-1);
		}
	}
	if(y+1<=a && x+1<=b){
		if(sima[y+1][x+1]==1){
			sima[y+1][x+1]=0;
			func(y+1,x+1);
		}
	}
	if(y+1<=a && x-1>=0){
		if(sima[y+1][x-1]==1){
			sima[y+1][x-1]=0;
			func(y+1,x-1);
		}
	}
}