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;

int h,w;
int mas[60][60];

void solve(int x,int y){
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      int nx = x + i;
      int ny = y + j;
      if(nx >= 0 && ny >= 0 && nx < w && ny < h && mas[ny][nx] == 1){
	mas[ny][nx] = 0;
	solve(nx,ny);
      }
    }
  }
  return;
}

int main(){
  while(cin >> w >> h , w){
    int cnt = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> mas[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(mas[i][j] == 1){
	  mas[i][j] = 0;
	  solve(j,i);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
}

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

void dfs(int y, int x, vector<vector<bool>> &used, vector<vector<int>> &M) {
    if (used[y][x]) return;
    used[y][x] = true;
    REP(i, -1, 2) {
        REP(j, -1, 2) {
            int ny = y + i, nx = x + j;
            if (ny < 0 || ny >= M.size() || nx < 0 || nx >= M[ny].size() || M[ny][nx] == 0) continue;
            dfs(ny, nx, used, M);
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int w, h;
    while (cin >> w >> h, w + h) {
        auto M = make_v(h, w, 0);
        REP(i, 0, h) {
            REP(j, 0, w) {
                cin >> M[i][j];
            }
        }
        auto used = make_v(h, w, false);
        int ans = 0;
        REP(i, 0, h) {
            REP(j, 0, w) {
                if (!used[i][j] && M[i][j] == 1) {
                    dfs(i, j, used, M);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: //41
#include<iostream>

using namespace std;

int w,h;
bool imap[50][50];

void ff(int y,int x){
  imap[y][x]=false;
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if(imap[y+i][x+j]){
	ff(y+i,x+j);
      }
    }
  }
}

int main(){
  for(;cin>>w>>h,w|h;){
    fill_n(&imap[0][0],50*50,false);
    for(int y=0;y<h;y++){
      for(int x=0;x<w;x++){
	cin>>imap[y][x];
      }
    }
    int ans=0;
    for(int y=0;y<h;y++){
      for(int x=0;x<w;x++){
	if(imap[y][x]){
	  ans++;
	  ff(y,x);
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

typedef long long itemType;

struct node
{long long v; struct node *next;};

void visit(stack<long long> stack, long long k, struct node *z, struct node **adj, long long id, long long val[]){
    struct node *t;
    t = new node;
    stack.push(k);
    while(!stack.empty()){
        k = stack.top();
        stack.pop();
        val[k] = ++id;
        for(t = adj[k]; t != z; t = t -> next){
            if(val[t -> v] == 0){
                stack.push(t -> v);
                val[t -> v] = -1;
            }
        }
    }
}

void search(stack<long long>stack, long long V, struct node *z, struct node **adj, long long id, long long val[], long long &comp, vector<vector<bool> > c, long long w){
    long long k;
    for(k = 1; k <= V; k++) val[k] = 0;
    for(k = 1; k <= V; k++){
        if(val[k] == 0){
            if(c[(k-1)/w + 1][((k-1)%w) + 1]) comp++;
            visit(stack, k, z, adj, id, val);
        }
    }
}

int main(){
    long long w, h;
    vector<bool> sub;
    bool c_sub;
    long long i, j;
    for(i = 0; i <= 50; i++){
        sub.push_back(0);
    }
    vector<vector<bool> > c;
    for(i = 0; i <= 50; i++){
        c.push_back(sub);
    }
    long long V;
    struct node *z;
    z = new node;
    z -> next = z;
    struct node *adj[2501];
    struct node *t;
    long long x, y;
    while(1){

        //input
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(i = 1; i <= h; i++){
            for(j = 1; j <= w; j++){
                cin >> c_sub;
                c[i][j] = c_sub;
            }
        }

        //adjacency list representation
        V = w * h;
        stack<long long> stack;
        for(i = 1; i <= V; i++){
            adj[i] = z;
        }
        for(j = 1; j <= w; j++){
            for(i = 1; i <= h-1; i++){
                if(c[i][j] & c[i+1][j]){
                    x = (i-1)*w + j;
                    y = i*w + j;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        
        for(i = 1; i <= h; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i][j] & c[i][j+1]){
                    x = (i-1)*w + j;
                    y = (i-1)*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i][j] & c[i+1][j+1]){
                    x = (i-1)*w + j;
                    y = i*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i+1][j] & c[i][j+1]){
                    x = i*w + j;
                    y = (i-1)*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }

        //depth first search
        long long id = 0;
        long long val[V + 1];
        long long comp = 0;
        search(stack, V, z, adj, id, val, comp, c, w);
        cout << comp << endl;
    }

    return 0;
}