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 <iostream>
#include <map>
// #include <cstdio>
#include <stack>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}

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

const int W = 50, H = 50;
int w, h;
int board[H][W];

void search(int y, int x){
	board[y][x] = 0;
	for(int dx = -1; dx <= 1;++dx){
		for(int dy = -1; dy <= 1;++dy){
			int nx = x + dx, ny = y + dy;
			if(0 <= ny && 0 <= nx && nx < w && ny < h){
				if(board[ny][nx]) search(ny, nx);
			}
		}
	}
	return;
}

int main(){
	int c;
	while(cin >> w >> h, w){
		c = 0;
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				cin >> board[i][j];
			}
		}
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				if(board[i][j]){
					search(i, j);
					++c;
				}
			}
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include <cstdio>

int map[50][50];
int w, h;
int ans;
int xx, yy;
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

void Walk(int X, int Y) {
	map[Y][X] = 0;
	for(int i = 0; i < (sizeof(dx) / sizeof(dx[0])); i++) {
		xx = X + dx[i];
		yy = Y + dy[i];
		if(0 <= xx && xx < w && 0 <= yy && yy < h && map[yy][xx] == 1) {
			Walk(xx, yy);
		}
	}
}

int main(void) {
	while(1) {
		ans = 0;
		scanf("%d %d", &w, &h);
		if(!w && !h) break;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				scanf("%d", &map[i][j]);
			}
		}
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] == 1) {
					ans++;
					Walk(j, i);
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<62;
const int INF = 1<<30;
const double PI=acos(-1);
int ans = 0;


void func(int h,int w,vector<vector<int>>&path,vector<vector<bool>>&has){
   
    
    //clog<<h<<" "<<w<<endl;
    if(path[h][w]==1 and has[h][w]==false and h<52 and w<52){
        path[h][w] = 0;
        has[h][w] = true;
        func(h+1,w,path,has);
        func(h + 1, w + 1, path, has);
        func(h, w + 1, path, has);
        if(h>0){
            func(h - 1, w, path, has);
            func(h - 1, w + 1, path, has);
        }
        if(w>0){
            func(h, w - 1, path, has);
            func(h + 1, w - 1, path, has);
        }
        if(h>0 and w>0){
            func(h - 1, w - 1, path, has);
        }
    }
}
int main()
{
    int h, w;

    while (1)
    {
        vector<vector<int>> path(60, vector<int>(60, 0));
        vector<vector<bool>> has(60, vector<bool>(60, false));

        ans = 0;

        rep(i, 59){
            rep(j, 59){
                path[i][j] = 0;
                has[i][j] = false;
            }
        }
        cin >> h >> w;
        if (h == 0 and w == 0)
        {
            break;
        }

        rep(i, w)
        {
            rep(j, h)
            {
                cin >> path[i][j];
            }
        }
        rep(i, w)
        {
            rep(j, h)
            {
                if (has[i][j] == false)
                {
                 //   clog<<i<<" "<<j<< " -> "<<path[i][j]<<endl;
                    if (path[i][j] == 1)
                    {
                        ans++;
                       
                    }
                    func(i, j, path, has);
                }
            }
        }
     //   clog<<'f'<<endl;
        cout << ans << endl;
    }
}

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

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout << is << endl;
    cin >> w >> h;
  }
  return 0;
}

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

C++: #include<iostream>
using namespace std;
int map[114][514];
int h, w;
void dfs(int i, int j) {
	map[i][j] = 0;
	if (i > 0) {
		if (j > 0) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j < w) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j > 0) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j < w) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i < h) {
		if (j > 0) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j < w) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)cin >> map[i][j];
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)if (map[i][j] == 1) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}