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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define int long long

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

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

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

signed main()
{
    while(cin>>w>>h, w) {
        for(int i=0; i<h; ++i)
            for(int j=0; j<w; ++j) field[i][j] = 0;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                cin >> field[i][j];
            }
        }

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

        cout << ans << endl;
    }
    return 0;
}

C++: 
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int H, W, m[55][55], dx[] = {1,1,0,-1,-1,-1,0,1}, dy[] = {0,1,1,1,0,-1,-1,-1};
bool flag[55][55];

void dfs(int y, int x) {
	for (int k = 0; k < 8; k++) {
		int nx = x + dx[k] , ny = y + dy[k];
		if (0<=ny && ny<H && 0<=nx && nx<W) {
			if (flag[ny][nx] && m[ny][nx]==1) {
				flag[ny][nx] = false;
				dfs(ny, nx);
			}
		}
	}
}

int main() {
	
	while (1) {

		cin >> W >> H;
		if (W==0 && H==0) break;
	
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> m[i][j];
				flag[i][j] = true;
			}
		}

		int cnt = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (m[i][j] == 1 && flag[i][j]) cnt++;
				flag[i][j] = false;
				if(m[i][j]==1) dfs(i, j);
			}
		}

		cout << cnt<<endl;

	}
	return 0;
}

C++: #include <stdio.h>
int i,j,RB[51][51],N,M,ans,a[8][2]={-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1};void bfs(int x, int y) {RB[x][y]=0;for (int q=0,nx,ny;q<8;q++) {nx=x+a[q][0],ny=y+a[q][1];if (nx>=0 && nx<N && ny>=0 && ny<M && RB[nx][ny]) bfs(nx,ny);}}
int main() {while (1) {scanf("%d %d",&M,&N);if (!N && !M) return 0;ans=0;for (i=0;i<N;i++) for (j=0;j<M;scanf("%d",&RB[i][j++]));for (i=0;i<N;i++) for (j=0;j<M;j++) if (RB[i][j]) bfs(i,j),ans++;printf("%d\n",ans);}}

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

int visited[50][50];
const int offset[8][2] = {
    {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}
};
int grid[50][50];

void dfs(int i, int j, int h, int w){
    for (int k = 0; k < 8; k++) {
        int ii = i + offset[k][0], jj = j + offset[k][1];
        if (ii < 0 || ii >= h || jj < 0 || jj >= w) {
            continue;
        }
        if (!visited[ii][jj] && grid[ii][jj]) {
            visited[ii][jj] = 1;
            dfs(ii, jj, h, w);
        }
    }
}


int main(int argc, char *argv[])
{
    
    cin.tie(0);
    ios::sync_with_stdio(false);

    int w, h;

    
    while (cin >> w >> h, w != 0 && h != 0) {
        memset(visited, 0, 50*50*sizeof(int));
        memset(grid, -1, 50*50*sizeof(int));
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> grid[i][j];
            }
        }
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (visited[i][j] || !grid[i][j]) {
                    continue;
                }
                dfs(i, j, h, w);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    
    return 0;
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

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

using namespace std;
int dx[8]={1,1,1 ,0, 0,-1,-1,-1};
int dy[8]={0,1,-1,1,-1, 1, 0,-1};
int c[55][55];
int w,h;
int res;
void paint(int i,int j,int k){
  if(i<0||j<0)return;
  else if(i>=h||j>=w) return;
  else if(c[i][j]!=1) return;
  c[i][j]=k;
  for(int l=0;l<8;l++) paint(i+dx[l],j+dy[l],k);
  return;
}
void count(int i,int j){
  res++;
  paint(i,j,res);
  return;
}
int main(void){
  int i,j;
  while(scanf("%d %d",&w,&h),w|h){
    res=1;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]==1){
          count(i,j);
        }
      }
    }
    printf("---\n");
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        printf("%d",c[i][j]);
      }
      printf("\n");
    }
    printf(":%d\n",res-1);
  }
  return 0;
}