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<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>

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

using namespace std;

int search_map(int x,int y,int flag_new,int m[50][50]){

  if(!(x>=0 && y<=w && y>=0 && y<=h)) return 0;

  if(history[x][y] == 1) return 0;
  else{
    history[x][y]=1;

    if(m[x][y]){

      if(flag_new) cnt++;

      for(int i=-1 ; i<2;i++){
        for(int j=-1 ; j<2;j++){
          search_map(x+i,y+j,0,m);
        }
      }

    }
    else{
      return 0;
    }

  }

}

int main(void){

  int map[50][50];

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

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        search_map(j,i,1,map);
        //cout << map[i][j] << " ";
      }
      //cout << endl;
    }
    cout << cnt << 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];
int 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]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	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 <algorithm>
#include <cstdio>

using namespace std;

#define int long long
#define dotimes(i, n) for (int i = 0; i < (n); i++)

int rint() {
  int n;
  scanf("%lld", &n);
  return n;
}

int w, h;
int c[50][50], d[50][50];

bool rec(int i, int j) {
  if (!c[i][j] || d[i][j])
    return false;
  d[i][j] = true;
  for (int y = -1; y <= 1; y++)
    for (int x = -1; x <= 1; x++)
      if ((x || y) && 0 <= i+y && i+y < h && 0 <= j+x && j+x < w)
        rec(i+y, j+x);
  return true;
};

signed main() {
  for (;;) {
    w = rint();
    h = rint();
    if (!w && !h)
      return 0;
    dotimes(i, h)
      dotimes(j, w) {
        c[i][j] = rint();
        d[i][j] = 0;
      }
    int r = 0;
    dotimes(i, h)
      dotimes(j, w)
        if (rec(i, j))
          r++;
    printf("%lld\n", r);
  }
}

C++: #include <iostream>
#include <vector>

using namespace std;

inline bool inRange(int a, int x, int b) {
    return a <= x && x < b;
}

void fill(vector<vector<int> > &v, int r, int c) {
    v[r][c] = 0;
    for(int dr = -1; dr <= 1; ++dr) {
        for(int dc = -1; dc <= 1; ++dc) {
            int nr = r + dr;
            int nc = c + dc;
            if(inRange(0, nr, v.size()) && inRange(0, nc, v[0].size()) && v[nr][nc]) {
                fill(v, nr, nc);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);

    while(true) {
        int C, R;
        cin >> C >> R;
        if(!C && !R) break;

        vector<vector<int> > v(R, vector<int>(C));
        for(int i = 0; i < R; ++i) 
            for(int j = 0; j < C; ++j)
                cin >> v[i][j];

        int cnt = 0;
        for(int r = 0; r < R; ++r) {
            for(int c = 0; c < C; ++c) {
                if(v[r][c]) {
                    ++cnt;
                    fill(v, r, c);
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<queue>
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++;
					queue<int> xs,ys;
					xs.push(j); ys.push(i);
					while(!xs.empty()) 
					{
						int x=xs.front(),y=ys.front();
						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>

using namespace std;
typedef long long int ll;
vector<vector<int>>v;
int w,h;

int dh[8] = {1,1,0,-1,-1,-1,0,1};
int dw[8] = {0,1,1,1,0,-1,-1,-1};

void dfs(int i, int j, int num)
{
    v[i][j] = 0;
    for(int k = 0; k < 8; k++)
    {
        int ni = i + dh[k];
        int nj = j + dw[k];
        if(ni >= h || ni < 0 || nj >= w || nj < 0) continue;
        if(!v[ni][nj]) continue;
        dfs(ni,nj,num);
    }
}

int main()
{
    while(cin >> w >> h)
    {
        if(w == 0 && h == 0) break;
        v.resize(h);
        for(int i = 0; i < h; i++)
        {
            v[i].resize(w);
            for(int j = 0; j < w; j++) cin >> v[i][j];
        }
        int num = 0;
        for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(v[i][j]) num++, dfs(i,j,num);
        cout << num << endl;
    }
}