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<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
void saiki(vector<vector<bool> >& in,int x,int y,int n,int m){
  in[x][y]=0;
  //cout<<x<<" "<<y<<endl;
  loop(i,-1,2){
    loop(j,-1,2){
      if(x+i>=0&&x+i<n&&y+j>=0&&y+j<m&&in[x+i][y+j])saiki(in,x+i,y+j,n,m);
    }
  }
}
int main(){
  int n,m;
  while(cin>>m>>n,n||m){
    int co=0;
    vector<vector<bool> >in(n);
    rep(i,n){
      rep(j,m){
	bool tmp;
	cin>>tmp;
	in[i].push_back(tmp);
      }
    }
    rep(i,n){
      rep(j,m){
	if(!in[i][j])continue;
	saiki(in,i,j,n,m);
	co++;
      }
    }
    cout<<co<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

void er(int c[52][52],int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(c,i+1,j-1);
    else if(c[i][j+1])
        er(c,i,j+1);
    else if(c[i+1][j])
        er(c,i+1,j);
    else if(c[i+1][j+1])
        er(c,i+1,j+1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    int c[52][52];
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(c,i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}

C++: // aribon2-2_f
#include <iostream>
#include <vector>

using namespace std;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define COUT(x) cout << x << endl

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

int W, H;
vector<vector<int>> C;

void dfs(int h, int w){
    C[h][w] = 0;
    REP(i, 8){
        int nh = h + dx[i];
        int nw = w + dy[i];
        if (C[nh][nw] == 0) continue;
        dfs(nh, nw);
    }
}

int main() {

    while (true){
        cin >> W >> H;
        if (W == 0 && H == 0) return 0;

        C.assign(H+2, vector<int>(W+2, 0));
        REP(i, H) REP(j, W){
            cin >> C[i+1][j+1];
        }

        int ans = 0;
        REPN(i, 1, H+1) REPN(j, 1, W+1){
            if (C[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        COUT(ans);
    }
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <iomanip>
#include <cassert>
#include <cmath>
using namespace std; typedef long long ll; ll an=0; const ll INF = 1e18; const ll MOD = 1e9+7;
typedef pair<int,int> P;
vector<int> v[50];

void dfs(int x, int y, int r, int c) {
  if (!v[x][y]) return;
  //cout<<"("<<x<<","<<y<<","<<r<<","<<c<<") ";
  v[x][y] = 0;
  for(int p=-1;p<=1;p++) {
    for(int q=-1;q<=1;q++) {
      int f=x+p, g=y+q;
      if (0 <= f && f < c && 0 <= g && g < r && v[f][g]) {
        dfs(f,g,r,c);
      }
    }
  }
}
int main(){
  while(true) {
    int r,c; cin>>r>>c; if (r==0) return 0;
    for(int i=0;i<c;i++) {v[i].clear(); v[i].resize(r);}
    int an=0;
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        int x; cin>>x; v[i][j]=x;
      }
    }
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        if (v[i][j]) {an++, dfs(i,j,r,c);}
      }
    }
    cout<<an<<endl;
  }
}

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

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

void dfs(vector< vector<int> > &plane, int x, int y){
  plane[x][y] = 0;
  for(int i=0; i<8; i++){
    if(0 <= x+dx[i] && x+dx[i] < h && 0 <= y+dy[i] && y+dy[i] < w)
      if(plane[x + dx[i]][y + dy[i]] == 1)
        dfs(plane, x + dx[i], y + dy[i]);
  }

  return;
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0)
      break;

    vector< vector<int> > plane(h, vector<int>(w));
    for(int i=0; i<h; i++)
      for(int j=0; j< w; j++)
        cin >> plane[i][j];

    int ans = 0;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if(plane[i][j] == 1){
          dfs(plane, i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  
  return 0;
}