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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_W 50
#define MAX_H 50

int w, h;
int maze[MAX_H][MAX_W];

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

vector<int> ans_list;

void solve(int x, int y){

	maze[y][x] = 0;

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 0 && nx < w && ny >= 0 && ny < h && maze[ny][nx] == 1)
			solve(nx, ny);
	}

}

int main(){

	while (true){
	
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

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

		int ans = 0;

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (maze[i][j] == 1){
					solve(j, i);
					ans++;
				}
			}
		}
	
		ans_list.push_back(ans);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i,n) for (int i=0; i < (n); ++i)
#define FOR(i,a,b) for (int i=(a); i < (b); ++i)
using namespace std;

int get_gcd(int a, int b){while(1){if (a < b) swap(a, b); if (b == 0) return a; a %= b;}}
int get_lcm(int a, int b){return a * b / get_gcd(a, b);}
//int dx[4]={1,0,-1,0};
//int dy[4]={0,1,0,-1};

//-------------------------テンプレここまで-------------------------//

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

int w, h;
// 0なら海、1なら陸（未探索)
bool field[50][50];
// 探索済かどうか
bool seen[50][50];

void dfs(int i, int j){
  seen[i][j] = true;

  REP(k,8) {
    int ni = i+dx[k];
    int nj = j+dy[k];
    if (ni<0 || ni>=h || nj<0 || nj>=w) continue;
    if (!field[ni][nj] || seen[ni][nj]) continue;
    dfs(ni, nj);
  }
}

signed main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  while(1) {
    cin >> w >> h;
    if(w==0 && h==0) break;

    REP(i,h) {
      REP(j,w) {
        cin >> field[i][j];
        seen[i][j] = false;
      }
    }

    int ans = 0;
    REP(i,h) {
      REP(j,w) {
        if (!field[i][j] || seen[i][j]) continue;
        dfs(i,j);
        ans++;
        //cout << i << " " << j << endl;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

#define rep(i,n) for (int i = 0; i < (n); ++i)

int w, h, c[55][55], ans;
bool is_search[55][55];
int vx[8] = {0, -1, -1, -1, 0, 1, 1, 1},
    vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct land_or_sea {
    int x, y, num;
    land_or_sea(int x, int y, int num) : x(x), y(y), num(num) {}
};

void bfs(int x, int y) {
    queue<pair<int, int> > que;
    que.push(make_pair(x, y));
    is_search[x][y] = true;

    while (!que.empty()) {
        pair<int, int> p = que.front(); que.pop();
        rep(i,8) {
            int nx, ny;
            nx = p.first + vx[i], ny = p.second + vy[i];
            if (0 <= nx && nx < w && 0 <= ny && ny < h &&
                c[nx][ny] == 1 && is_search[nx][ny] == false) {
                   que.push(make_pair(nx, ny));
                   is_search[nx][ny] = true;
            }
        }
    }

}

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (cin >> w >> h, w | h) {
        memset(c, 0, sizeof(c));
        memset(is_search, false, sizeof(is_search));
        ans = 0;
        rep(i,h) {
            rep(j,w) {
                cin >> c[i][j];
            }
        }

        rep(i,w) {
            rep(j,h) {
                if (is_search[i][j] == false && c[i][j] == 1) {
                    bfs(i,j);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>

int history[50][50];
int w,h;
int cnt;

using namespace std;

int search_map(int x,int y,int flag_new,int m[50][50]){

  if(!(x>=0 && x<w && y>=0 && y<h)) return 0;

  if(history[y][x] == 1) return 0;
  else{
    history[y][x]=1;

    if(m[y][x]){

      if(flag_new) cnt++;

      for(int i=-1 ; i<2;i++){
        for(int j=-1 ; j<2;j++){
          if(i==0 && j==0) continue;
          search_map(x+i,y+j,0,m);
        }
      }

    }
    else{
      return 0;
    }

  }

}

int main(void){

  int map[50][50];

  for(cin>>w,cin>>h;w>0 && h>0;cin>>w,cin>>h){
    cnt=0;
    for(int i=0;i<50;i++){
      for(int j=0;j<50;j++){
        history[i][j]=0;
      }
    }
    //cout << w << "," << h << endl;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> map[i][j];
        //cout << map[i][j] << " ";
      }
      //cout << endl;
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        search_map(j,i,1,map);
        //cout << map[i][j] << " ";
      }
      //cout << endl;
    }
    cout << cnt << endl;
  }

  return 0;
}

C++: #include <iostream>

using namespace std;
int c[51][51];
int w ,h;

bool input(){
	cin >> h >> w;
	if(w==0) return false;
	for(int i = 0;i <51;i++){
		for(int j = 0;j <51 ;j++){
			c[i][j] = 2;
		}
	}

	for(int i = 0;i < w ;i++){
		for(int j = 0; j < h ;j++){
			cin >> c[i][j];
//			cout <<c[i][j];
		}
	}
	return true;
}

void dfs(int x,int y){
	c[x][y] = 0;
	for(int i = -1; i <=1 ;i++){
		for(int j = -1; j <=1 ;j++){
			int nx = x + i;
			int ny = y + j;
//			cout <<nx << ' ' << ny << endl;
			if(0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1)dfs(nx,ny);
		}
	}
}

void solve(){
	int res = 0;
/*	for(int i=0;i < w ;i++){
		for (int j=0;j<h;j++){
			cout <<c[i][j];
		}
		cout <<endl;
	}
*/
	for(int i=0; i < h;i++){
		for(int  j=0;j < w;j++){
			if(c[i][j] ==1){
				res++;
				dfs(i,j);
			}
		}
	}
	cout << res << endl;
}

int main(){
	while(input())solve();
}