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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)),(x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

const int MAX = 64;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const int MOD = 1000000007;

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

int maze[MAX][MAX];

int H, W, ans;

bool range(int y, int x) {
    return (0 <= x && x < W && 0 <= y && y < H);
}

void dfs(int y, int x){
    maze[y][x] = 0;
    rep(8, d) {
        if (range(y+dy[d], x+dx[d]) && maze[y+dy[d]][x+dx[d]]) {
            dfs(y+dy[d], x+dx[d]);
        }
    }
    return;
}

void solve(void){
    while (1) {
        //input & init
        scanf("%d %d\n", &W, &H);
        if (W == 0 && H == 0) break;
        rep(H, y) rep(W, x) scanf("%d", &maze[y][x]);
        ans = 0;

        rep(H, y) rep(W, x) {
            if (maze[y][x]) {
                dfs(y, x);
                ans++;
            }
        }
        printf("%d\n", ans);
    }
}

int main(void){
  solve();
  return 0;
}

C++: #include <iostream>

using namespace std;

int w, h, cnt;
int island[50][50];
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || w <= ny || ny < 0 || h < ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

  while(cin >> w >> h, w + h){

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

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

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

using namespace std;
typedef vector<vector<int> > field;

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

void erase(field &f, int y, int x)
{
    f[y][x] = 0;
    for(int i=0; i<8; i++)
    {
        if(f[y+dy[i]][x+dx[i]]) erase(f, y+dy[i], x+dx[i]);
    }
}

int main()
{
    int w, h;
    while(cin >> w >> h, w|h)
    {
        field f(h+2, vector<int>(w+2, 0));
        for(int y=1; y<=h; y++)
        {
            for(int x=1; x<=w; x++)
            {
                int c;
                cin >> c;
                f[y][x] = c;
            }
        }
        int ct = 0;
        for(int y=1; y<=h; y++)
        {
            for(int x=1; x<=w; x++)
            {
                if(f[y][x])
                {
                    ct++;
                    erase(f, y, x);
                }
            }
        }
        cout << ct << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int hx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int hy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
vector<vector<int>> mp;
vector<vector<bool>> used;
int w, h;

void dfs(int i, int j) {
  if (used[i][j])
    return;
  used[i][j] = true;
  for (int k = 0; k < 8; k++) {
    int ni = i + hx[k], nj = j + hy[k];
    if (ni >= h || ni < 0 || nj >= w || nj < 0)
      continue;
    if (used[ni][nj] || mp[ni][nj] == 0)
      continue;
    dfs(ni, nj);
  }
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    mp = vector<vector<int>>(h, vector<int>(w));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    used = vector<vector<bool>>(h, vector<bool>(w));
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (used[i][j])
          continue;
        if (mp[i][j] == 0)
          continue;
        dfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
  }
}

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

vector<int> route;
#define P 50
#define Q 50
char a[P+2][Q+2];


void search(int place1,int place2){
    a[place1][place2] = '0';
    if(a[place1][place2-1] == '1'){//左
        route.push_back(1);
        search(place1,place2-1);
    }
    else if(a[place1+1][place2] == '1'){//下
        route.push_back(2);
        search(place1+1,place2);
    }
    else if(a[place1][place2+1] == '1'){//右
        route.push_back(3);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2] == '1'){//上
        route.push_back(4);
        search(place1-1,place2);
    }
    else if(a[place1+1][place2-1] == '1'){//左下
        route.push_back(5);
        search(place1+1,place2-1);
    }
    else if(a[place1+1][place2+1] == '1'){//右下
        route.push_back(6);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2+1] == '1'){//右上
        route.push_back(7);
        search(place1-1,place2);
    }
    else if(a[place1-1][place2-1] == '1'){//左上
        route.push_back(8);
        search(place1-1,place2);
    }
    else{
        int bk = route.back();
        route.pop_back();//戻る
        if(bk == 0) return;
        else if(bk == 1)search(place1,place2+1);
        else if(bk == 2)search(place1-1,place2);
        else if(bk == 3)search(place1,place2-1);
        else if(bk == 4)search(place1+1,place2);
        else if(bk == 5)search(place1-1,place2+1);
        else if(bk == 6)search(place1-1,place2-1);
        else if(bk == 7)search(place1+1,place2-1);
        else if(bk == 8)search(place1+1,place2+1);
    }
}

int main(void){
    int ct;
    int x,y;
    while(1){
        ct = 0;
        cin >> y >> x;
        if(x == 0 & y == 0) break;
        for(int i = 0;i < x+2;i++){
            for(int j = 0;j < y+2;j++){
                if(i == 0||j == 0||i == x+1||j == y+1)a[i][j] = '0';
                else cin >> a[i][j];
                
            }
            
        }
        for(int i = 1;i < x+1;i++){
            for(int j = 1;j < y+1;j++){
                if(a[i][j] == '1'){
                    route.push_back(0);
                    search(i,j);
                    ct++;
                }
            }
        }
        cout << ct << endl;
    }
    return 0;
}