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>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};
int cf[100][100] = {};
int LUT[1000] ={};



int main(){

    while(1){
        scanf("%d %d",&w,&h);
        if(w == 0 && h==0)break;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&f[i][j]);
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(f[i][j] == 1 && cf[i][j]==0){
                    ans++;
                    LUT[ans]=ans;
                    cf[i][j] = ans;
                }
                
                for(int k=-1;k<2;k++){
                    if(i+k<0 || h <= i+k)continue;
                    for(int t=-1;t<2;t++){
                        if(j+t<0 || w <= j+t)continue;
                        if(f[i][j] == 1 && f[i+k][j+t]==1 ){
                            if(cf[i+k][j+t]==0){
                                cf[i+k][j+t]= cf[i][j];
                            }else if(cf[i+k][j+t] > cf[i][j]){
                                LUT[cf[i+k][j+t]] = cf[i][j];
                            }
                        }
                    }
                }
            }
        }
        for(int i=0;i<1000;i++){
            if(LUT[i] != 0 && LUT[i] != i){
                ans--;
            }
        }
        cout << ans << endl;
    }

}

C++: #include<stdio.h>
int main(){
  int w,h,a[51][51],i,j,i2,j2,c,k;
  while(1){
    //syokika
    for(i=0;i<51;i++){
      for(j=0;j<51;j++){
	a[i][j]=0;
      }
    }
    c=2;
  
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%d",&a[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]==1){
	  a[i][j]=c;
	  k=1;
	  while(k!=3){
	    k=1;
	    //go
	    for(i2=0;i2<h;i2++){
	      for(j2=0;j2<w;j2++){
		//someru
		if(a[i2][j2]==1){
		  if(j2!=0){
		    if(a[i2][j2-1]==c){
		      a[i2][j2]=c; //left
		      k=2;
		    }
		  }
		  if(i2!=0){
		    if(a[i2-1][j2]==c){
		      a[i2][j2]=c; //up
		      k=2;
		    }		    

		    if(j2!=0){
		      if(a[i2-1][j2-1]==c){
			a[i2][j2]=c; //left up
			k=2;
		      }
		    }
		    if(a[i2-1][j2+1]==c){
		      a[i2][j2]=c; //right up
		      k=2;
		    }
		    
		  }
		}
		//some end
	     
	      }
	    }

	    //back
	    for(i2=h-1;i2>=0;i2--){
	      for(j2=w-1;j2>=0;j2--){
		//someru
		if(a[i2][j2]==1){
		  if(a[i2][j2+1]==c){
		    a[i2][j2]=c; //right
		    k=2;
		  }    
		  if(a[i2+1][j2]==c){
		    a[i2][j2]=c; //down
		    k=2;
		  }		
		  if(a[i2+1][j2+1]==c){
		    a[i2][j2]=c; //right do
		    k=2;
		  }
		  if(j2!=0){
		    if(a[i2+1][j2-1]==c){
		      a[i2][j2]=c; //left do
		      k=2;
		    }
		  }
		}
		
		
		//some end
		
	      }
	    }
	    if(k==1)k=3;
	    //sima end
	  }
	  c++;
	}
      }
    }
    printf("%d\n",c-2);
  }
  return 0;
}

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

int W, H;

int tansaku(vector<vector<int>> &data, int h, int w) {
    int ret = 0;
    if(data[h][w] == 1) {
        ret = 1;
        data[h][w] =0;
        if(h-1 >= 0          ) ret |= tansaku(data, h-1, w);
        if(h+1 <  H          ) ret |= tansaku(data, h+1, w);
        if(           w-1 >=0) ret |= tansaku(data, h  , w-1);
        if(           w+1 < W) ret |= tansaku(data, h  , w+1);
        if(h-1 >=0 && w-1 >=0) ret |= tansaku(data, h-1, w-1);
        if(h-1 >=0 && w+1 < W) ret |= tansaku(data, h-1, w+1);
        if(h+1 < H && w-1 >=0) ret |= tansaku(data, h+1, w-1);
        if(h+1 < H && w+1 < W) ret |= tansaku(data, h+1, w+1);
    }
    return ret;
}

int func() {
    cin >> W >> H;
    if(W==0 && H==0) return 0;
    //int data[H][W];
    vector<vector<int>> data;
    data.resize(H);

    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            int tmp;
            cin >> tmp;
            //data[h][w] = tmp;
            data[h].push_back(tmp);
        }
    }


    int counter = 0;
    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            counter += tansaku(data, h, w);
        }
    }
    cout << counter << endl;
    
    return 1;
}

int main() {
    while(func()) {}
    return 0;
}

C++:  #include<bits/stdc++.h>
 using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int w,h;
int c[101][101];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,0,1,-1,0,1,-1};
void dfs(int x,int y){
     c[x][y]=0;
     REP(i,8){
         if(c[x+dx[i]][y+dy[i]]==1){
            dfs(x+dx[i],y+dy[i]);
         }
     }
}
int main(){
    while(cin>>w>>h,w,h){
    REP(i,h+1){
        REP(j,w+1){
            c[i][j]=0;
        }
    }
    REP(i,h){
        REP(j,w){
            cin>>c[i+1][j+1];
        }
    }
    int ans=0;
    REP(i,h+1){
        REP(j,w+1){
            if(c[i][j]==1){
               dfs(i,j);
               ans++;
            }
        }
    }
    cout<<ans<<endl;
    }
    return 0;
}