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;

typedef long long ll;
typedef pair<int,int> P;

int c[100][100];//0??????
int h,w;

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

void bfs(int i,int j){
    queue<P> q;
    q.push(P(i,j));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        c[x][y]=0;
        for(int i=0;i<8;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]==1){
                q.push(P(nx,ny));
            }
        }
    }
}

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

  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>>c[i][j];
      }
      int res=0;
      for(int i=0;i<h;i++)for(int j=0;j<w;j++){
          if(c[i][j]==1){
              bfs(j,i);
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include <iostream>

#define MAX 51

int map[MAX][MAX];
int w, h;

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


int main()
{

    int cnt;

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

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

        std::cout << cnt << std::endl;

    }

return 0;
}

C++: #include <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}}if(w==0)a++,w=i*n+j+1;else w=0;d[j]=b;b=w;}d[n]=b;w=0;}printf("%d\n",a);main();}

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

const int hmax = 50;
int w, h;
void move(string area[], int i, int j){
	area[i][j] = '0';
	for(int k = -1; k <= 1; k++){
		for(int l = -1; l <= 1; l++){
			if(i+k < h && i+k >= 0 && j+l < w && j+l >= 0)
				if(area[i+k][j+l] == '1') move(area,i+k,j+l);
		}
	}
}

int main(){
	while(cin >> w >> h){
		string area[hmax];
		if(w == 0 && h == 0) break;
		for(int i = 0; i < h; i++){
			string s;
			for(int j = 0; j < w; j++){
				char c;
				cin >> c;
				s.push_back(c);
			}
			area[i] = s;
		}
		int count = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(area[i][j] == '1'){
					count++;
					move(area,i,j);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

int main() {
	int w, h;
	while (cin >> w >> h, h || w) {
		vector<vector<int>> c(h, vector<int>(w));
		for (vector<int>& i : c) {
			for (int& j : i) cin >> j;
		}
		int ans = 0;
		vector<vector<bool>> a(h, vector<bool>(w));
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (!a[i][j] && c[i][j]) {
					++ans;
					a[i][j] = true;
					queue<pair<int, int>> b;
					b.emplace(i, j);
					while (!b.empty()) {
						if (b.front().first) {
							if (!a[b.front().first - 1][b.front().second] && c[b.front().first - 1][b.front().second]) {
								a[b.front().first - 1][b.front().second] = true;
								b.emplace(b.front().first - 1, b.front().second);
							}
							if (b.front().second) {
								if (!a[b.front().first - 1][b.front().second - 1] && c[b.front().first - 1][b.front().second - 1]) {
									a[b.front().first - 1][b.front().second - 1] = true;
									b.emplace(b.front().first - 1, b.front().second - 1);
								}
							}
							if (b.front().second != w - 1) {
								if (!a[b.front().first - 1][1 + b.front().second] && c[b.front().first - 1][1 + b.front().second]) {
									a[b.front().first - 1][1 + b.front().second] = true;
									b.emplace(b.front().first - 1, 1 + b.front().second);
								}
							}
						}
						if (b.front().first != h - 1) {
							if (!a[1 + b.front().first][b.front().second] && c[1 + b.front().first][b.front().second]) {
								a[1 + b.front().first][b.front().second] = true;
								b.emplace(1 + b.front().first, b.front().second);
							}
							if (b.front().second != w - 1) {
								if (!a[1 + b.front().first][1 + b.front().second] && c[1 + b.front().first][1 + b.front().second]) {
									a[1 + b.front().first][1 + b.front().second] = true;
									b.emplace(1 + b.front().first, 1 + b.front().second);
								}
							}
						}
						if (b.front().second) {
							if (!a[b.front().first][b.front().second - 1] && c[b.front().first][b.front().second - 1]) {
								a[b.front().first][b.front().second - 1] = true;
								b.emplace(b.front().first, b.front().second - 1);
							}
							if (b.front().first != h - 1) {
								if (!a[1 + b.front().first][b.front().second - 1] && c[1 + b.front().first][b.front().second - 1]) {
									a[1 + b.front().first][b.front().second - 1] = true;
									b.emplace(1 + b.front().first, b.front().second - 1);
								}
							}
						}
						if (b.front().second != w - 1) {
							if (!a[b.front().first][1 + b.front().second] && c[b.front().first][1 + b.front().second]) {
								a[b.front().first][1 + b.front().second] = true;
								b.emplace(b.front().first, 1 + b.front().second);
							}
						}
						b.pop();
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

using namespace std;

queue<int> Q;
int A[52][52];
int UP[8]  ={-1, -1, 1, 0, 1, 1, 1, 0};
int SIDE[8]={-1, 0, 1, 1, 1, 0, -1, -1};


void route(int w,int h){

  A[w][h]=0;
  if(A[w+1][h+1]==1)route(w+1,h+1);
  if(A[w+1][h]==1)route(w+1,h);
  if(A[w+1][h-1]==1)route(w+1,h-1);
  if(A[w][h+1]==1)route(w,h+1);
  if(A[w][h-1]==1)route(w,h-1);
  if(A[w-1][h+1]==1)route(w-1,h+1);
  if(A[w-1][h]==1)route(w-1,h);
  if(A[w-1][h-1]==1)route(w-1,h-1);


}

int main(){
  int w,h;
  int count;
  while(true){
    cin>>w>>h;
    if(w==0&&h==0)break;
    for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	cin>>A[i][j];
     
      }
    }
    count=0;
    for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	if(A[i][j]==1){
	  //	  cout<<i<<" "<<j<<endl; 
	  route(i,j);
	  count++;
	}
      }
    }

    //デバック
   
    /*     for(int i = 1;i<=h;i++){
      for(int j = 1;j<=w;j++){
	cout<<" "<<A[i][j];
      }
      cout <<endl;
      }*/
    
    cout<<count<<endl;
   }
  return 0;

}