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