Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    //ofstream outputfile("output.txt");
    int w, h;
    while(cin >> w >> h, w + h) {
        if(w == 0 && h == 0) break;
        int temp;
        rep(i, h) {
            rep(j, w) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
vector<vector<char>> m;int w,h;
void search(int y,int x){
    if(y<0 || x<0 || y>=h || x>=w || m[y][x] == '0' || m[y][x] == 'c') return;
    m[y][x] = 'c';
    search(y+1,x);
    search(y-1,x);
    search(y,x+1);
    search(y,x-1);
    search(y+1,x+1);
    search(y+1,x-1);
    search(y-1,x+1);
    search(y-1,x-1);
    return;
}
int main(){
    int ans,i,j;vector<char> emp;char s;
    while(true){
        cin >> w >> h;
        if(w==0 && h==0) break;
        m = {};
        for(i=0;i<h;i++){
            m.push_back(emp);
            for(j=0;j<w;j++) {
                cin >> s;
                m[i].push_back(s);
            }
        }
        ans = 0;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(m[i][j] == '1'){
                    search(i,j);ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>

using namespace std;

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

void dfs(int x, int y)
{
    m[y][x] = 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) {
        int 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) {
                    dfs(i, j);
                    isl++;
                }

        cout << isl << endl;
    }

    return 0;
}

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

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

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

int main(int argc, char const *argv[])
{
	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 >> c[i][j];

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<utility>
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int gx[8] = {1,1,0,-1,-1,-1,0,1};
int gy[8] = {0,1,1,1,0,-1,-1,-1};
#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;

int nextInt(){int a;cin>>a;return a;}

int main(){
  int w,h,ct;
  
  queue<pair<int,int>> que;
  while(cin>>h>>w){
    if(!h&&!w)break;
    vector<vector<int>> d(w,vector<int>(h,-1));
    REP(i,w){
      REP(j,h){
	d[i][j] = nextInt();
      }
    }
    ct = 0;
    REP(i,w){
      REP(j,h){
	int def_i = i;
	int def_j = j;
	if(d[def_i][def_j] !=1)continue;
	else ct++;
	  que.push(make_pair(def_i,def_j));
	  while(que.size()){
	    pair<int,int> p = que.front();
	    que.pop();
	    REP(k,8){
	      def_i = gy[k] + p.first;def_j = gx[k] + p.second;
	      if(def_i>=0 && def_i<w && def_j>=0 && def_j<h){
		if(d[def_i][def_j] !=1)continue;
		else {
		  d[def_i][def_j] = -1;
		  que.push(make_pair(def_i,def_j));
		}
	      }
	    }
	  }
      }
    }
    cout << ct << endl;
    
  }
	
	return 0;
}

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

const int MAX = 50;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };
int map[MAX][MAX];
int color[MAX][MAX];

void dfs(int x, int y, int c)
{
	color[x][y] = c;

	for (int i = 0; i < 8; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (nx >= 0 && nx < MAX&&ny >= 0 && ny < MAX) {
			if (map[nx][ny] == 1) {
				if (color[nx][ny] == 0) {
					dfs(nx, ny, c);
				}
			}
		}
	}
}

int main()
{
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0 && h == 0) {
			break;
		}

		for (int i = 0; i < MAX; i++) {
			for (int j = 0; j < MAX; j++) {
				map[i][j] = 0;
				color[i][j] = 0;
			}
		}

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

		int ans = 0;

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

		cout << ans << endl;
	}

	return 0;
}