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 <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

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

int W,H;
int dx[]={-1,0,1,0,-1,-1,1,1};
int dy[]={0,-1,0,1,-1,1,-1,1};
int f[50][50];

void dfs(int x, int y)
{
	f[x][y]=0;
	for(int i=0; i<8; i++)
	{
		int tx=x+dx[i], ty=y+dy[i];
		if(tx<0||ty<0||tx>=W||ty>=H) continue;
		if(f[tx][ty]==0) continue;
		
		dfs(tx,ty);
	}
}

int main()
{
	while(scanf("%d%d", &W,&H), (W||H))
	{
		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
			scanf("%d", &f[j][i]);
			
		int ans=0;
		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
		{
			if(f[j][i])
			{
				ans++;
				dfs(j,i);
			}
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <iostream>

using namespace std;

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

int w, h;
bool visited[50][50] = {};
int m[50][50];

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

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 >> m[i][j];
    }
  }

  

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

  cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

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

#define rep(i,a) for(int i=0;i<(a);++i)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

int w, h;
int field[51][51];
int dx[] = { -1, -1, -1, 0, 1, 1, 1,  0};
int dy[] = { -1,  0,  1, 1, 1, 0, -1, -1};

bool infield(int x, int y)
{
    return x >= 1 && x <= w && y >= 1 && y <= h;
}

int bfs(int sx, int sy)
{
    queue<P> que;
    que.push(P(sx, sy));
    while(!que.empty()) {
        P p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if(!infield(x, y) || field[y][x] == 0)
            continue;

        field[y][x] = 0;
        for(int i = 0; i < 8; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            que.push(P(nx, ny));
        }
    }
}

int main()
{
    while(cin >> w >> h && w && h) {
        for(int i = 1; i <= h; i++) 
            for(int j = 1; j <= w; j++) 
                cin >> field[i][j];

        int ans = 0;
        for(int i = 1; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                if(field[i][j] == 1) { 
                    bfs(j, i);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int w, int h){
	field[h][w] = 0;

	for(int nh=h-1;nh<h+2;nh++){
	    for(int nw=w-1;nw<w+2;nw++){
	      if(field[nh][nw]==1&&0<=nh&&0<=nw&&nh<h&&nw<w)dfs(nw,nh);
	    }
	  }
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			break;
		}

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

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					count++;
					dfs(j,i);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define prime 1000000007
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
bool Grid[51][51];
int w, h;
void dfs(int x, int y) {
    //cout << x << ':' << y << "HaHaHaHa" << endl;
    rep(i, 8) {
        int nx = x + dx8[i];
        int ny = y + dy8[i];
        //cout << x << ':' << dx8[i] << ' ' << y << ':' << dy8[i] << ' ' << nx << ':' << ny << endl;
        if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
        if(Grid[nx][ny]) {
            Grid[nx][ny] = false;
            dfs(nx, ny);
        }
    }
}
signed main(void) {
    while(true) {
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        rep(i, 51) rep(j, 51) Grid[i][j] = false;
        rep(i, h) rep(j, w) {
            int x;
            cin >> x;
            if(x == 1) Grid[i][j] = true;
        }
        int ans = 0;
        rep(i, h) rep(j, w) {
            if(Grid[i][j]) {
                ans++;
                Grid[i][j] = false;
                dfs(i, j);
            }
        }
        cout << ans << endl;
    }
}

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

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

void dfs(int y, int x)
{
	f[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];
		if(f[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}

int main()
{
	int w,h,c,x,y;
	while(cin >> w >> h, w||h)
	{
		memset(f,0,sizeof(f));

		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			cin >> f[y][x];
		}

		c=0;
		for(y=1; y<=h; y++)
		for(x=1; x<=w; x++)
		{
			if(f[y][x])
			{
				c++;
				dfs(y,x);
			}
		}
		cout << c << endl;
	}
}