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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int w, h;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    int mp[55][55];
    bool used[55][55];

    queue<pair<int, int>> que;
    rep(y, h) {
      rep(x, w) {
        cin >> mp[y][x];
        if (mp[y][x] == 0)
          used[y][x] = false;
        else
          used[y][x] = true;
      }
    }

    int ans = 0;

    rep(y, h) {
      rep(x, w) {
        if (used[y][x]) {
          ans++;
          que.push(make_pair(x, y));
          while (!que.empty()) {
            pair<int, int> p = que.front();
            que.pop();

            rep(i, 8) {
              int nx = p.first + ddx[i];
              int ny = p.second + ddy[i];
              if (0 <= nx && nx < w && 0 <= ny && ny < h && used[ny][nx]) {
                used[ny][nx] = false;
                que.push(make_pair(nx, ny));
              }
            }
          }
        }
      }
    }

    cout << ans << "\n";
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;


int n,m;
int dy[8]={0,1,-1,0,1,-1,1,-1},dx[8]={1,0,0,-1,1,1,-1,-1};
int t[100][100];
bool u[100][100]={};
stack<pair<int,int>>S;
void DFS(int a,int b)
{
	S.push(make_pair(a,b));
	u[a][b]=true;
	while(!S.empty())
	{	
		bool flag=true;
		int h=S.top().first,w=S.top().second;
		for(int i=0;i<8;i++)
		{
			int p=h+dy[i],q=w+dx[i];
			if(t[p][q]==1&&!u[p][q])
			{
				S.push(make_pair(p,q));
				u[p][q]=true;
				flag=false;
			}
		}
		if(flag)S.pop();
	}
}

int main()
{	
	while(cin>>n>>m)
	{
		int ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<=m+1;i++)
		{
			for(int j=0;j<=n+1;j++)
			{
				if(i==0||i==m+1||j==0||j==n+1)t[i][j]=0;
				else cin>>t[i][j];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(u[i][j]||t[i][j]==0)continue;
				ans++;
				DFS(i,j);
			}
		}
		cout<<ans<<endl;
		for(int i=0;i<100;i++)
		{
			for(int j=0;j<100;j++)u[i][j]=false;
		}
	}
	return 0;
}

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

int w,h;
int filed[51][51];

void dfs(int x,int y){
  filed[x][y]=0;
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      int nx=x+dx,ny=y+dy;
      if(0<=nx&&nx<h&&0<=ny&&ny<w&&filed[nx][ny]==1)dfs(nx,ny);
    }
  }
}

void solve(){
  int res=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(filed[i][j]==1){
	dfs(i,j);
	res++;
      }
    }
  }
  cout<<res<<endl;
}

int main(){
  
while(1){
    cin>>w>>h;
    if(w+h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>filed[i][j];
      }
    }
    solve();
  }
  return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={-1,0,1,-1,1,-1,0,1},DY[]={-1,-1,-1,0,0,1,1,1};

int w, h;
VII cs;

void dfs(int y, int x){
	int ans = 0;
	cs[y][x] = 0;
	rep(i, 8){
		int ny = y + DY[i], nx = x + DX[i];
		if(range(ny, 0, h - 1) && range(nx, 0, w - 1) && cs[ny][nx]){
			dfs(ny, nx);
		}
	}
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1){
		cs.clear();
		int ans = 0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		rep(i, h){
			VI c;
			rep(j, w){
				int a;
				cin >> a;
				c.pb(a);
			}
			cs.pb(c);
		}
		rep(i, cs.size()){
			rep(j, cs[i].size()){
				if(!cs[i][j]) continue;
				dfs(i, j);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}