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<iostream>
using namespace std;

int w,h;
bool map[50][50];

void Del(int x,int y){

  map[y][x]=false;

  if(x!=w-1){
    if(map[y][x+1]==true){
      Del(x+1,y);
    }

    if(y!=0){
      if(map[y-1][x+1]==true){
        Del(x+1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x+1]==true){
        Del(x+1,y+1);
      }
    }

  }


  if(x!=0){
    if(map[y][x-1]==true){
      Del(x-1,y);
    }
    if(y!=0){
      if(map[y-1][x-1]==true){
        Del(x-1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x-1]==true){
        Del(x-1,y+1);
      }
    }

  }


  if(y!=h-1){
    if(map[y+1][x]==true){
      Del(x,y+1);
    }
  }
  if(y!=0){
    if(map[y-1][x]==true){
      Del(x,y-1);
    }
  }

}


int main(){


  int sum;

  while(cin >> w >> h){
    if(w==0 && h==0){ break; }

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

    sum = 0;

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if(map[i][j]==1){
          sum++;
          Del(j,i);
        }
      }
    }

    cout << sum << endl;

  }



  return 0;
}

C++: #include <bits/stdc++.h>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};

int solve(int i,int j,int num){
    if(i<0 || j < 0 || i >= h || j >= w )
        return 0;
    if(f[i][j]!=1)
        return 0;
    
    f[i][j]=num;
    solve(i-1,j-1,num);
    solve(i-1,j,num);
    solve(i-1,j+1,num);
    solve(i,j-1,num);
    solve(i,j+1,num);
    solve(i+1,j-1,num);
    solve(i+1,j,num);
    solve(i+1,j+1,num);
    return 0;
}


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 = 1;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(f[i][j]==1){
                    ans++;
                    solve(i,j,ans);
                }
            }
        }
        cout << ans-1 << endl;
    }

}

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

int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, W, H;
bool mem[50][50];

int dfs(int x, int y)
{
  if(!mem[y][x])
    return 0;
  mem[y][x] = 0;
  for(int i = 0; i < 8; i++)
  {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if(tx >= 0 && tx < W && ty >= 0 && ty < H && mem[ty][tx])
      dfs(tx, ty);
  }
  return 1;
}

int main()
{
loop:
  int ans = 0;
  for(int y = 0; y < 50; y++)
    for(int x = 0; x < 50; x++)
      mem[y][x] = 0;
  cin >> W >> H;
  if(!W)
    return 0;
  for(int y = 0; y < H; y++)
    for(int x = 0; x < W; x++)
      cin >> mem[y][x];
  for(int y = 0; y < H; y++)
    for(int x = 0; x < W; x++)
      ans += dfs(x, y);
  cout << ans << endl;
  goto loop;
}

C++: #include<iostream>
#include<math.h>
#include<vector>
using namespace std;

int w,h;
int c[60][60];
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,1,1,1,0,-1,-1,-1};
bool seen[60][60];

void func(int i,int j){
    seen[i][j]=true;

    for(int n=0;n<8;++n){
        int ii=i+dy[n];
        int jj=j+dx[n];

        if(ii<0||jj<0||ii>=h||jj>=w)continue;
        if(c[ii][jj]==0)continue;
        if(seen[ii][jj])continue;
        func(ii,jj);
    }
}

int main(){
    while(cin>>w>>h && w!=0){
    int num=0;
    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            cin>>c[i][j];
            seen[i][j]=false;
        }    
    }

    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            if(c[i][j]==1&&!seen[i][j]){
                func(i,j);
                num++;
            }
        }    
    }
    cout<<num<<endl;}
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];

int mv[3] = {0,-1,1};

void dfs(int x,int y){
  if(dp[x][y] == -1)return;
  dp[x][y] = -1;
  rep(i,0,3){
    rep(j,0,3){
      if(mv[i] == 0 && mv[j] == 0)continue;
      if(dp[x+mv[i]][y+mv[j]] == 1)dfs(x + mv[i],y + mv[j]);
    }
  }
}

int main(){
  while(1){
    cin >> m >> n;
    if(!n && !m)break;
    clr(dp,-1);
    lep(i,1,n){
      lep(j,1,m){
        cin >> dp[i][j];
      }
    }
    ll ans = 0;
    lep(i,1,n){
      lep(j,1,m){
        if(dp[i][j] == 1){
          ans++;
          dfs(i,j);
        }
      }
    }
    print(ans);
  }
  return 0;
}