Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: #include <bits/stdc++.h>

// using namespace std;

using map_t = std::vector<std::vector<bool>>;

void fill(const map_t& map, int w, int h, int x, int y, map_t& visited) {
  if (x < 0 || w <= x || y < 0 || h <= y) {
    return;
  }
  if (!map[y][x] || visited[y][x]) {
    return;
  }
  visited[y][x] = true;
  fill(map, w, h, x - 1, y - 1, visited);
  fill(map, w, h, x + 0, y - 1, visited);
  fill(map, w, h, x + 1, y - 1, visited);
  fill(map, w, h, x - 1, y + 0, visited);
  fill(map, w, h, x + 1, y + 0, visited);
  fill(map, w, h, x - 1, y + 1, visited);
  fill(map, w, h, x + 0, y + 1, visited);
  fill(map, w, h, x + 1, y + 1, visited);
}

int solve(const map_t& map, int w, int h) {
  map_t visited(h, std::vector<bool>(w, false));
  int ret = 0;
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; x++) {
      if (map[y][x] && !visited[y][x]) {
        ret++;
        fill(map, w, h, x, y, visited);
      }
    }
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  while (true) {
    int w, h;
    std::cin >> w >> h;
    if (w == 0 || h == 0) {
      break;
    }
    map_t map(h, std::vector<bool>(w));
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; x++) {
        int a;
        std::cin >> a;
        map[y][x] = a != 0;
      }
    }
    std::cout << solve(map, w, h) << std::endl;
  }
  return 0;
}

C++: #include<cstdio>
int n, m,ans;
int a[100][100];
bool b[100][100];

void dfs(int x,int y) {
	b[x][y] = true;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (0 <= x + i&&x + i < n) {
				if (0 <= y + j&&y + j < m) {
					if (a[x+i][y+j] == 1 && !b[x+i][y+j])dfs(x+i, y+j);
				}
			}
		}
	}
	return;
}

int main(void) {

	while (true) {
		scanf("%d %d", &m, &n);
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) {
				a[i][j] = 0;
				b[i][j] = false;
			}
		}
		for (int i = 0; i < n;i++) {
			for (int j = 0; j < m; j ++) {
				scanf("%d", &a[i][j]);
			}
		}
		ans = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (a[i][j] == 1 && !b[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int w,h;
int field[51][51];
void dfs(int x,int y);

int main(){
	while(cin>>w>>h&&w!=0&&h!=0){
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				cin>>field[y][x];
			}
		}
		int count=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(field[y][x]==1){
					dfs(x,y);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}

}

void dfs(int x,int y){
	field[y][x]=-1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int mx=x+i;
			int my=y+j;

			if(mx<0||my<0||mx>=w||my>=h)continue;
			if(field[my][mx]==1){
				dfs(mx,my);
			}
		}
	}
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+2, y);
    dfs(x+2, y+1);
    dfs(x+2, y-1);
    dfs(x-2, y);
    dfs(x-2, y+1);
    dfs(x-2, y-1);
    dfs(x, y+1);
    dfs(x, y-1);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            scanf("%s\n", c[k]);
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for(int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

struct UF { // O(loga(n))
    int n;
    int m;
    vi d, r;
    UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
    int root(int i) {
        if(d[i] < 0) return i;
        return d[i] = root(d[i]);
    }
    bool same(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return false;

        if(r[x] < r[y])
            swap(x, y);
        else if(r[x] == r[y])
            r[x]++;
        d[x] += d[y];
        d[y] = x;
        --m;
        return true;
    }
    int size(int i) { return -d[root(i)]; }
    int size() { return m; }
};

int main() {
    int w, h;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;
        vector<vi> d(h, vi(w));
        rep(i, h) rep(j, w) cin >> d[i][j];
        UF uf(h * w);
        rep(i, h) rep(j, w) {
            if(d[i][j]) REP(dy, -1, 2) REP(dx, -1, 2) {
                int x = j + dx, y = i + dy;
                if(min(x, y) < 0 || x >= w || y >= h || d[y][x] == 0)
                    continue;
                uf.unite(i * w + j, y * w + x);
            }
        }
        int ans = 0;
        rep(i, h) rep(j, w) if(d[i][j] && uf.root(i * w + j) == i * w + j) {
            ++ans;
            // cout << " " << i << " " << j << endl;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[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;
		if (c[ny][nx])
			dfs(nx, ny);
	}
}

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

int main() {
	while (1) {
		
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				c[i][j] = 0;
		}
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}