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;
using ll=long long;
#define fi first
#define se second
using Graph=vector<vector<int>>;
using P=pair<int,int>;
#define mp make_pair

void dfs(int u,int v,Graph graph,Graph &d,int flag)
{
	int h,w;
	h=graph.size();
	w=graph[0].size();
	int n=graph.size();
	d[u][v]=flag;
	vector<P> around={mp(u-1,v),mp(u+1,v),mp(u-1,v-1),mp(u-1,v+1),mp(u,v+1),mp(u,v-1),mp(u+1,v+1),mp(u+1,v-1)};
	for (int i=0;i<8;i++)
	{
		int x=around[i].fi,y=around[i].se;
		if (0<=x&&x<h && 0<=y&&y<w)
		{
			if (graph[x][y]==1 && d[x][y]==-1)
			{
				//cout << x << y << endl;
				dfs(x,y,graph,d,flag);
			}

		}
	}
}

int main()
{
	vector<int> ans;
	while (true)
	{
		int w,h;cin >> h >> w;
		if (w==0 && h==0) break;
		Graph graph(w,vector<int> (h,0)),d(w,vector<int> (h,0));
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				cin >> graph[i][j];
				if (graph[i][j]==1) d[i][j]=-1;
			}
		}
		int flag=1;
		for (int i=0;i<w;i++)
		{
			for (int j=0;j<h;j++)
			{
				if (d[i][j]==-1)
				{
					dfs(i,j,graph,d,flag);
					flag++;
				}
			}
		}
		ans.push_back(flag-1);
	}
	for (int i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w);
int a,n,m,d[99][99],u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};void k(int x,int y){if(d[x][y]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}}int main(){a=0;s(n)s(m)if(n==0)return 0;f(9801)d[0][i]=0;f(n*m)s(d[i/n+1][i%n+1])f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);printf("%d\n",a);main();}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int W,H;

int t[50][50];

void visit(int h,int w){
  t[h][w]=0;
  for(int nh=h-1;nh<h+2;nh++){
    for(int nw=w-1;nw<w+2;nw++){
      if(t[nh][nw]==1&&0<=nh&&0<=nw&&nh<H&&nw<W)visit(nh,nw);
    }
  }
}

int main(void){
  int a,b,c,n;
  while(1){
    cin>>W>>H;
    c=0;
    if(W==0&&H==0)break;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    cin>>t[i][j];
      }
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
    if(t[i][j]==1){
      c++;
      visit(i,j);
    }
      }
    }
    cout<<c<<endl;
  }
}

C++: #include <iostream>
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

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

int dx[] = {1,1,0,-1,-1,-1,0,1};
int dy[] = {0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y) {
    c[y][x] = 0;
    
    rep(i,8) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1) dfs(nx,ny);
    }
}
    
int main(void){
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(y,h) rep(x,w) cin >> c[y][x];
        
        int ans = 0;        
        rep(y,h)
            rep(x,w)
                if (c[y][x] == 1) {
                    dfs(x,y);
                    ans++;
                }

        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
bool find1(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  for(int i = 0;i < C.size();++i)
    for(int j = 0;j < C[0].size();++j)
      if(C[i][j] == 1 && check[i][j] == 0){
	*findh = i;
	*findw = j;
	return true;
      }
  return false;
}
bool find2(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  if(C[*findh - 1][*findw - 1] == 1 && check[*findh - 1][*findw - 1] == 0){
    *findh -= 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw] == 1 && check[*findh - 1][*findw] == 0){
    *findh -= 1;
    return true;
  }
  else if(C[*findh - 1][*findw + 1] == 1  && check[*findh - 1][*findw + 1] == 0){
    *findh -= 1;
    *findw += 1;
    return true;
  }
  else if(C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0){
    *findw -= 1;
    return true;
  }
  else if(C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0){
    *findw += 1;
    return true;
  }
  else if(C[*findh + 1][*findw - 1] == 1 && check[*findh + 1][*findw - 1] == 0){
    *findh += 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0){
    *findh += 1;
    return true;
  }
  else if(C[*findh + 1][*findw + 1] == 1 && check[*findh + 1][*findw + 1] == 0){
    *findh += 1;
    *findw += 1;
    return true;
  }
  else return false;
}
int main()
{
  int w,h,findh,findw;
  stack< pair<int,int> > finish;
  queue<int> answer;
  while(cin >> w >> h && !(w == 0 && h == 0)){
    int howmany = 0;
    vector< vector<int> > C(h + 2,vector<int>(w + 2,0)),check(h + 2,vector<int>(w + 2,0)); 
    for(int i = 1;i <= h;++i)
      for(int j = 1;j <= w;++j)
	cin >> C[i][j];
    while(find1(C,check,&findh,&findw)){
      check[findh][findw] = 1;
      finish.push(make_pair(findh,findw));
      while(!finish.empty()){
	findh = finish.top().first;
	findw = finish.top().second;
	while(find2(C,check,&findh,&findw)){
	  finish.push(make_pair(findh,findw));
	  check[findh][findw] = 1;
	}
	finish.pop();
      }
      ++howmany;
    }
    answer.push(howmany);
  }
  for(;!answer.empty();answer.pop())
    cout << answer.front() << endl;
}