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 rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

int w, h;
char c[51][51];
bool f[51][51];
int ans = 0;


bool contain(int y, int x){
    return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    if(c[y][x] == '0') return;
    rep(i, 0, 3){
        rep(j, 0, 3){
            int ny = y + i - 1;
            int nx = x + j - 1;
            if(contain(ny, nx) && f[ny][nx] == false){
                dfs(ny, nx);
            }
        }
    }
    return;
}

signed main(){
    while(cin >> w >> h){
        if(w + h == 0) break;
        rep(i, 0, h){
            rep(j, 0, w){
                cin >> c[i][j];
            }
        }
        rep(i, 0 ,51){
            rep(j, 0, 51){
                f[i][j] = false;
            }
        }
        // cout << -1 << endl;
        ans = 0;
        rep(i, 0, h){
            rep(j, 0 ,w){
                if(c[i][j] == '1' && f[i][j] == false){
                    // cout << i << ' ' << j << endl;
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }

}

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

using namespace std;
using ll = long long;

int dir[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, -1, -1, 0, 1, 1}};

void dfs(int i, int j, vector<vector<int>>& used, vector<vector<int>>& color){
  used[i][j] = 1;
  for(int d = 0; d < 8; d++){
    if(i + dir[0][d] >= 0 && i + dir[0][d] < used.size() && j + dir[1][d] >= 0 && j + dir[1][d] < used[0].size()){
      if(!used[i+dir[0][d]][j+dir[1][d]] && color[i+dir[0][d]][j+dir[1][d]]){
        dfs(i + dir[0][d], j + dir[1][d], used, color);
      }
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int w, h;
  while(cin >> w >> h && w){
    vector<vector<int> > used(h, vector<int>(w, 0));
    vector<vector<int> > color(h, vector<int>(w, 0));
    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> color[i][j];
      }
    }
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!used[i][j] && color[i][j]) ans++, dfs(i, j, used, color);
      }
    }
    cout << ans << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}

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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

int main(){
	while(cin >> w >> h){
		if(w == 0&&h==0) break;
		ans = 0;
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				cin >> c[i][j];
			}
		}
		for(int i = 1;i<=h;i++){
			for(int j = 1;j<=w;j++){
				ans+=hantei(i,j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int a[55][55]={};
int w,h;
int count=0;
void dfs(int y,int x){
  a[y][x]=0;
  int dy[8]={-1,-1,-1,0,0,1,1,1};
  int dx[8]={-1,0,1,-1,1,-1,0,1};
  for(int i=1;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx<0||ny<0||nx>=w||ny>=h)continue;
    if(a[ny][nx]==0)continue;
    dfs(ny,nx);
  }
}
int main(){
  while(1){
    int ans=0;
    cin>>w>>h;
    if(w==0&&h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>a[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(a[i][j]==0)continue;
	ans++;
	dfs(i,j);
      }
    }
    cout<<ans<<endl;
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
//?¶????????????????????????????????????§??????????????????????¢????????????????
constexpr int INF = 2000000000;
const double PI = acos(-1);

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

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

int main() {
	while (cin >> w >> h&&w&&h) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> maze[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (maze[i][j] == 1) {
					ans++;
					dfs(i, j);
				}
			}
		}
		cout << ans << endl;
	}

}