Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

struct dsu {
  const int n;
  vector<int> p, sz;
  dsu(int _n) : n(_n), p(n, -1), sz(n, 1) {}
  int root(int v) { return p[v] == -1 ? v : v = root(p[v]); }
  void unite(int u, int v) {
    u = root(u), v = root(v);
    if (u == v) return;
    if (sz[u] < sz[v]) swap(u, v);
    p[v] = u;
    sz[u] += sz[v];
  }
  int size(int v) { return sz[root(v)]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  while (cin >> w >> h, w) {
    vector<vector<int>> a(h, vector<int>(w));
    for (auto&& v : a) {
      for (auto&& e : v) {
        cin >> e;
      }
    }
    dsu d(h * w);
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (a[i][j]) {
          for (int di = -1; di <= 1; ++di) {
            for (int dj = -1; dj <= 1; ++dj) {
              if (di or dj) {
                int ni = i + di, nj = j + dj;
                if (0 <= ni and ni < h and 0 <= nj and nj < w and a[ni][nj]) {
                  d.unite(i * w + j, ni * w + nj);
                }
              }
            }
          }
        }
      }
    }
    set<int> se;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (a[i][j]) {
          se.insert(d.root(i * w + j));
        }
      }
    }
    cout << se.size() << '\n';
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <math.h>
using namespace std;

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

int w,h,cnt,l[50][50];

void f(int x,int y){
	l[y][x]=0;
	for(int i=0;i<8;i++){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx>=0&&nx<w&&ny>=0&&ny<h&&l[ny][nx]==1)f(nx,ny);
	}
}

int main(){
	while(cin>>w>>h,w||h){
		cnt=0;
		for(int i=0;i<w*h;i++)cin>>l[i/w][i%w];
		for(int i=0;i<w*h;i++)if(l[i/w][i%w]==1){cnt++;f(i%w,i/w);}
		cout<<cnt<<endl;
	}
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

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

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> c;
				a[i][j] = c;
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < s2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}

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

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;


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

void dfs(vector<vector<int> > &map,int x,int y){
    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/
    //今いるところを-100に置き換える
    map[x][y] = -100;
    //移動方向をループ
    for(int dx = -1;dx <= 1;dx++){
        for(int dy = -1;dy <= 1;dy++){
            //x方向にdx,ｙ方向にdy移動した場所を(nx,ny)とする
            int nx = x + dx, ny = y + dy;
            if(0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1){
                dfs(map,nx,ny);
            }
        }
    }
    return;
}

int solve(vector<vector<int> > &map,int h,int w){

    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/

    int ans = 0;
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(map[i][j] == 1){
                //Wが残っているならばその場所からdfsを始める
                dfs(map,i,j);
                //ここで1つの水たまりが見つかったことになるので水たまりの個数を１つ足す
                ans++;
            }
        }
    }
    return ans;
}

int main(){

    
    

    while(cin >> w >> h){

        if(w == 0 && h == 0){
            break;
        }
        int ans = 0;
        vector<vector<int> > map(h,vector<int>(w));//周波数ベクトル列データ用の2次元配列

        for(int i = 0;i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> map[i][j];
                //cout << map[i][j] << " " ;
            }
            //cout << endl;
        }
        //cout << endl;

        ans = solve(map,h,w);

        cout << ans << endl;
    }

   


    

    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
bool used[55][55];
int n,m;

void check(int x,int y){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(used[x][y])return;
	used[x][y]=true;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			check(x+i,y+j);
		}
	}
}

int main(){
	int i,j,k;
	int a,b,c;
	while(cin>>m>>n){
		if(!n)break;
		memset(used,false,sizeof(used));
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				cin>>a;
				if(a==0)used[i][j]=true;
			}
		}
		a=0;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				if(!used[i][j])check(i,j),a++;
			}
		}
		cout<<a<<endl;
	}
}