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

int w;
int h;
char field[1145][1419];
bool used[1145][1419];

void dfs(int y, int x) {
  if (y < 0 || h <= y) return;
  if (x < 0 || w <= x) return;
  if (field[y][x] == '0') return;
  if (used[y][x]) return;
  used[y][x] = true;

  for (int dy=-1; dy<=1; dy++) {
    for (int dx=-1; dx<=1; dx++) {
      if (dy == 0 && dx == 0) continue;
      int newy = y + dy;
      int newx = x + dx;
      dfs(newy, newx);
    }
  }
}

int main() {
  while (1) {
    if (scanf("%d%d", &w, &h) != 2) return 0;
    if (w == 0 && h == 0) return 0;

    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        scanf(" %c", &field[i][j]);
        used[i][j] = false;
      }
    }

    int ans = 0;
    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        if (!used[i][j] && field[i][j] == '1') {
          dfs(i, j);
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
}

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

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
typedef long long ll;

vector<vector<ll>> tizu(50,vector<ll>(50));
vector<vector<bool>> visit(50,vector<bool>(50, false));
ll h,w;

void dfs(pair<ll,ll> v)
{
    ll a = v.first;
    ll b = v.second;
    if(!tizu.at(a).at(b)){
        return;
    }
    visit.at(a).at(b) = true;
    ll tate[9] = {-1,0,1,-1,0,1,-1,0,1};
    ll yoko[9] = {-1,-1,-1,0,0,0,1,1,1};
    ll i,j;
    for(i = 0;i < 9;++i){
        ll p = a + tate[i];
        ll q = b + yoko[i];
        if(p < 0 || p >= h){
            continue;
        }else if (q < 0 || q >= w){
            continue;
        }else if(!visit.at(p).at(q) && tizu.at(p).at(q)){
            dfs(make_pair(p,q));
        }
    }
    return;
}

int main()
{
    ll i,j;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            return 0;
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                cin >> tizu.at(i).at(j);
            }
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                visit.at(i).at(j) = false;
            }
        }
        ll ans = 0;
        for(i = 0;i < h;++i){
            rep(j,w){
                if(visit.at(i).at(j) || !tizu.at(i).at(j)){
                    continue;
                }else{
                    dfs(make_pair(i,j));
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int c[51][51];
bool used[51][51];
const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={1,0,-1,1,-1,1,0,-1};
int main(){
    int w,h;
    while(cin>>w>>h,w|h){
        int cnt=0;
        memset(used,0,sizeof(used));
        rep(i,h)rep(j,w) cin>>c[i][j];
        rep(i,h)rep(j,w)if(!used[i][j]&&c[i][j]==1){
            ++cnt;
        queue<pair<pint,int> > q;
        q.push({{j,i},0});
        used[i][j]=true;
        while(!q.empty()){
            pair<pint,int> pi=q.front();q.pop();
            rep(k,8){
                int tx=pi.first.first+dx[k],ty=pi.first.second+dy[k];
                if(tx>=0&&ty>=0&&tx<w&&ty<h&&c[ty][tx]==1&&!used[ty][tx]){
                    q.push({{tx,ty},pi.second+1});
                    used[ty][tx]=true;
                }
            }
        }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i][j-1])a[k][l]=tmp;}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j-1])a[k][l]=tmp;}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j])a[k][l]=tmp;}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j+1])a[k][l]=tmp;}}
					}
				}
			}
		}
		
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}