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 <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

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

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                cin >> m[i][j];

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
                    isl++;
                    dfs(i, j);
                }
            
        cout << isl << endl;
    }

    return 0;
}

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

int checkMap(int w, int h, int map[52][52]){
	if(map[w][h]){
		map[w][h]=0;
		 checkMap(w+1, h+1, map);
		 checkMap(w+1, h, map);
		 checkMap(w+1, h-1, map);
		 checkMap(w, h-1, map);
		 checkMap(w-1, h-1, map);
		 checkMap(w-1, h, map);
		 checkMap(w-1, h+1, map);
		 checkMap(w, h+1, map);
		 return 1;
	}else return 0;
		 
}


int main(){
	int w,h;

	while(cin >> w >> h, w){
	int map[52][52] = {{0}};
		int ans=0;
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> map[i][j];
			}
		}
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
			ans += checkMap(i, j, map);
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w == 0 || h == 0){
    }
    else{
        cout << ans << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <string>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1, 1, -1,-1,1};
int dy[] = {1, 0, -1, 0, 1, 1,-1,-1};
#define INF 1000000000000
#define MAX 200001
#define MOD 1000000007
ll fac[MAX], finv[MAX], inv[MAX];
const int MX = 1000005;
vector<int> d(100,0);
vector<int> f(100,0);

void dfs(int x,int y,Graph &G,Graph &visited){
    visited[y][x]=1;
    for(int A=0;A<8;A++){
        int n_x=x + dx[A];
        int n_y=y + dy[A];
        if(n_x<0 or 49<n_x or n_y<0 or 49<n_y) continue;
        if(G[n_y][n_x]==1&&visited[n_y][n_x]==0){
            dfs(n_x,n_y,G,visited);
        }
    }
}

int main(){
    vector<int> res;
    while(true){
        vector<vector<int>> visited(50,vector<int>(50,0));
        int w,h;cin>>w>>h;
        int ans=0;
        if(w==0&&h==0) break;
        vector<vector<int>> G(50,vector<int>(50,0));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin>>G[i][j];
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(G[i][j]==1&&visited[i][j]==0){
                    dfs(j,i,G,visited);
                    ans++;
                }
            }
        }
        res.push_back(ans);
    }
    int s=res.size();
    for(int i=0;i<s;i++) cout<<res[i]<<endl;
}

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

using namespace std;

struct node{
  int x,y;
};

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

int main(){
  int w,h,i,j,k,ans=0;
  while(1){
    queue<node> que;
    scanf("%d %d",&w, &h);
    if(w+h == 0)break;
    int c[h][w];
    ans = 0;
    node now,next,to;
    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]){
          ++ans;
          now.x = j; now.y = i;
          que.push(now);
          while(!que.empty()){
            next = que.front();
            c[next.y][next.x] = 0;
            que.pop();
            for(k=0;k<8;k++){
              to.x = next.x + dx[k];
              to.y = next.y + dy[k];
              if(to.x<0 || to.x >=w || to.y<0 || to.y >=h || c[to.y][to.x]==0)continue;
              que.push(to);
            }
          }
        }
      }
    }
    printf("%d",ans);
  }
  return 0;
}