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 <iostream>
#include <queue>
#include <utility>

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0);

int main(){
  int w,h;
  while(cin>>w>>h&&w>0&&h>0){

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

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }
  }
}

C++: #include <iostream>
#include <stack>
using namespace std;

struct pos{
    int x;
    int y;
};

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        stack<pos> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    pos p;
                    p.y = i;
                    p.x = j;
                    st.push(p);
                    field[i][j] = 0;
                    while(!st.empty()){
                        pos stTop = st.top();
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            p = stTop;
                            p.y += dy[k];
                            p.x += dx[k];
                            if(field[p.y][p.x] == 1){
                                st.push(p);
                                field[p.y][p.x] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

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

using namespace std;

int w;
int h;
bool a[100][100];
int counter = 0;
int hor[3] = {-1,0,1};
int ver[3] = {-1,0,1};
int cnt = 0;
int i;
int j;
int i2;
int j2;

void dfs(int row,int col){
  if(row>=h || col>=w || row<0 || col<0 || a[row][col]==0){
    return;
  }
  a[row][col] = 0;
  for(int i2=0;i2<3;i2++){
    for(int j2=0;j2<3;j2++){
      dfs(row+hor[i2],col+ver[j2]);
    }
  }
}
int main() {
  while(1){
    cnt = 0;
    cin >>w;
    cin >>h;
    if(w==0 || h==0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
      cin>>a[i][j];
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(a[i][j]==1){
          cnt++;
          dfs(i,j);
        }
      }
    }
    printf("%d\n",cnt);
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

vector< vector<int> > M;
int h,w;

bool dfs(int y, int x){
    if(y>=h||y<0||x>=w||x<0)return false;
    if(M[y][x]==0)return false;
    M[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            dfs(y+i,x+j);
        }
    }
    return true;
}

int ans(){
    int res=0;
    REP(i,h)REP(j,w){
        if(dfs(i,j))res++;
    }
    
    return res;
}

int main(){
    
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        
        M.clear();
        M.resize(h);
        REP(i,h){
            REP(j,w){
                int t; cin>>t;
                M[i].push_back(t);
            }
        }
        
        cout<<ans()<<endl;
        
    }
    
    return 0;
}

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

using namespace std;

struct UFTree{
	vector<int> p;
	UFTree(int n):p(n,-1){}
	void Unite(int a,int b){
		int ra=Find(a),rb=Find(b);
		if(ra!=rb){
			if(-p[ra]<-p[rb])
				swap(ra,rb);
			p[ra]+=p[rb];
			p[rb]=ra;
		}
	}
	int Find(int n){
		return p[n]<0?n:p[n]=Find(p[n]);
	}
};

int main()
{
	for(int w,h;cin>>w>>h,w|h;){
		int grid[50][50];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				cin>>grid[i][j];
		UFTree uft(h*w);
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++){
				if(!grid[i][j])
					continue;
				int di[]={0,1,1,1},dj[]={1,-1,0,1};
				for(int k=0;k<4;k++){
					int ni=i+di[k],nj=j+dj[k];
					if(0<=ni && ni<h && 0<=nj && nj<w && grid[ni][nj])
						uft.Unite(i*w+j,ni*w+nj);
				}
			}
		int res=0;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				if(grid[i][j] && uft.p[i*w+j]<0)
					res++;
		cout<<res<<endl;
	}
	return 0;
}