Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

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

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

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

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