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

using namespace std;
#define ll long long
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
const ll mod = 1e9 + 7;


int w;
int h;
vector<vector<int> > c(50, vector<int>(50));
vector<vector<bool> > reached(50, vector<bool>(50,false));

void dfs(int y, int x){
    reached.at(y).at(x) = true;

    for(int dy = -1; dy <= 1; dy++){
        for(int dx = -1; dx <= 1; dx++){
            int nx = x + dx;
            int ny = y + dy;
			if(ny < 0 || h <= ny || nx < 0 || w <= nx || c.at(ny).at(nx) == 0) continue;          
            if(reached.at(ny).at(nx)) continue;
            dfs(ny,nx);

       }
    }
    
}




int main (){
while(cin>>w>>h,w!=0&&h!=0){
	
  	
    REP(i,h){//y
        REP(j,w){//x
          	reached.at(i).at(j) = false;
            cin >> c.at(i).at(j);
        }
    }
    int ans = 0;
    REP(i,h){
        REP(j,w){
            if(!reached.at(i).at(j) && c.at(i).at(j) == 1){
                dfs(i,j);
                ans++;
            }
            
        }
    }

    cout << ans << endl;




}

    return 0;
}

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

int c[100][100],ans;

int search(int x, int y){
	if(c[x][y] == 1){
		c[x][y] = 2;
		search(x-1, y-1);
		search(x-1, y);
		search(x-1, y+1);
		search(x, y-1);
		search(x, y+1);
		search(x+1, y-1);
		search(x+1, y);
		search(x+1, y+1);
	return 1;	
	}
	else return 0;
}

int main(){
	int w,h,i,j;
	while(cin >> w >> h && w > 0 && h >0){
		for(i = 1; i <= 99; i++){
			for(j = 1; j <= 99; j++){
				c[i][j] = 0;
			}
		}
		ans = 0;
		for(i = 1; i <= h; i++){
			for(j = 1; j <= w; j++){
				cin >> c[i][j];
			}
		}
		for(i = 1; i <= h; i++){
			for(j = 1; j <= w; j++){
				if(search(i, j) == 1){
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i = 0 ; i < n ; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main()
{
	debug_io
	int w, h;
	while (cin >> w >> h, w + h) {
		int tile[55][55];
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

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

		int ans = 0;
		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] == 1) {
					vector<pair<int, int>> bfs;
					bfs.push_back({ i,j });

					while (bfs.size()) {
						auto now = bfs.front();
						bfs.erase(bfs.begin());
						int x = now.second;
						int y = now.first;
						if (tile[y][x] == 2) {
							continue;
						}
						tile[y][x] = 2;
						REP(k, 8) {
							int nx = max(0,min(x + dx[k],w-1));
							int ny = max(0, min(y + dy[k], h - 1));
							if (tile[ny][nx] == 1) {
								bfs.push_back({ ny,nx });
							}
						}
					}
					ans++;
					
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define SCANF(i) int i;scanf("%d",&i)
using namespace std;
using P = pair<int,int>;
const long long INF = 1LL << 60;
const int IINF=100000000;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};
int h,w;
vector<vector<int>> grid;
vector<vector<bool>> seen;
int xx[8] = {0,-1,-1,-1,0,1,1,1};
int yy[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y){
    seen[x][y] = true;
    for(int i=0;i<8;i++){
        int nx = x+xx[i];int ny = y+yy[i];
        if(nx<0||nx>h-1||ny<0||ny>w-1)continue;
        if(!seen[nx][ny] && grid[nx][ny]==1){
            dfs(nx,ny);
        }
    }
}
signed main () {
    while(cin >> w >> h){
        if(h+w==0)break;
        int cnt = 0;
        grid.assign(h,vector<int>(w));
        seen.assign(h,vector<bool>(w,false));
        REP(i,h)REP(j,w)cin >> grid[i][j];
        REP(i,h)REP(j,w){
            if(grid[i][j]==1 && !seen[i][j]){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
}

C++: // Time-stamp: <2012-06-26 20:15:18 naga>

#include<iostream>
#include<cstdio>
using namespace std;
int maps[52][52];
int w,h;
void init(){
  for(int i = 0; i < 52; i++){
    for(int j = 0; j < 52; j++){
      maps[i][j] = 0;
    }
  }
  return;
}

void search(int x, int y){
  maps[y][x] = 0;
  if(maps[y - 1][x] && y > 0){
    search(x, y -1);
  }
  if(maps[y - 1][x - 1] && x > 0 && y > 0){
    search(x - 1, y - 1);
  }
  if(maps[y][x - 1] && x > 0){
    search(x - 1, y);
  }
  if(maps[y + 1][x - 1] && y < h && x > 0){
    search(x - 1, y + 1);
  }
  if(maps[y + 1][x] && y < h){
    search(x, y + 1);
  }
  if(maps[y + 1][x + 1] && y < h && x < w){
    search(x + 1, y + 1);
  }
  if(maps[y][x + 1] && x < w){
    search(x + 1, y);
  }
  if(maps[y - 1][x + 1] && y > 0 && x < w){
    search(x + 1, y - 1);
  }
  return;
}


int main(){
  int count;
  while(true){
    cin >> w >> h;
    if( !w && !h){
      break;
    }
    count = 0;
    init();
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> maps[i][j];
      }
    }
    
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	if(maps[i][j]){
	  count++;
	  search(j,i);
	}
      }
    }

    printf("%d\n", count);
  }
  return 0;
}