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>

using namespace std;

bool map[50][50];
int h,w;
int dx[]={-1,0,1},dy[]={-1,0,1};
void solve(void);
void loop(int ,int );
void draw(void);
int main(){
	while(cin>>h>>w,h){
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cin>>map[i][j];
			}
		}
		solve();
	}
	return 0;
}
void solve(void){
	int cnt=0;
	for(int i=0;i<w;i++){
		for(int j=0;j<h;j++){
			if(map[i][j]){
				loop(i,j);	
				cnt++;	
			}
		}
	}
	cout<<cnt<<endl;
}
void loop(int x,int y){
	map[x][y]=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			
			if(map[x+dx[i]][y+dy[j]]){
				loop(x+dx[i],y+dy[j]);
			}
		}
	}
	return ;
}

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

void dfs(vector<vector<int> > &map, int x, int y){
    map[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nx = x + dx, ny = y + dy;
            if(1 <= nx && nx <= map.size()-2 && 1 <= ny && ny <= map[dx].size()-2 && map[nx][ny] == 1) dfs(map, nx, ny);
        }
    }
}

int main(){
    while(1){
        int W, H;
        cin >> W >> H;
        if(W == 0) break;
    
        vector<vector<int> > map(H+2, vector<int>(W+2));
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                cin >> map[i][j];
            }
        }
    
        int res = 0;
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(map[i][j] == 1){
                    dfs(map, i, j);
                    res++;
                }
            }
        }
    
        cout << res << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int w,h;
vvll a(50,vll(50));

void dfs(int x,int y){
     a.at(x).at(y)=0;
     rep2(dx,-1,2){
       rep2(dy,-1,2){
         int  nx=x+dx;
         int  ny=y+dy;
           if(0<=nx&&nx<h&&0<=ny&&ny<w){
                if(a.at(nx).at(ny)==1){
                    dfs(nx,ny) ;         
                           }
                       }
                   }
               }                                  
}


int main() {
   while(1){
   cin>>w>>h;
   if(w==0&&h==0)break;
   else{
   rep(i,h){
       rep(j,w)cin>>a.at(i).at(j);
   }
  
   int ans=0;
   rep(i,h){
       rep(j,w){
           if(a.at(i).at(j)==1){
               
               
               dfs(i,j);
               ans++;
               
           }
       }
   }
   
  
   cout<<ans<<endl;
   }
   }
    
}

C++: #include<iostream>
#include<stack>
using namespace std;
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};
int main()
{
	int n,m;
	while(cin>>n>>m,n) 
	{
		int f[m][n];
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				cin>>f[i][j];
			}
		}

		int ans=0;
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(f[i][j]==1)
				{
					ans++;
					stack<int> xs,ys;
					xs.push(j); ys.push(i);
					while(!xs.empty()) 
					{
						int x=xs.top(),y=ys.top();
						xs.pop(); ys.pop();
						f[y][x]=0;
						for(int k = 0; k < 8; ++k)
						{
							int nx=x+dx[k],ny=y+dy[k];
							if(0<=nx&&nx<n&&0<=y&&y<m&&f[ny][nx]==1)
							{
								xs.push(nx); ys.push(ny);
							}
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
  return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

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

void solve(int x, int y)
{
    if (x < 0 || h <= x || y < 0 || w <= y)
        return;
    if (c[x][y] == 0)
        return;
    c[x][y] = 0;
    for (int i = -1; i < 2; i++)
    {
        for (int j = -1; j < 2; j++)
        {
            solve(x + i, y + j);
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (1)
    {
        cin >> w >> h;
        if (h == 0)
            break;
        rep(i, h)
        {
            rep(j, w)
            {
                cin >> c[i][j];
            }
        }
        int ans = 0;
        rep(i, h)
        {
            rep(j, w)
            {
                if (c[i][j] == 1)
                {
                    solve(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}