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<fstream>

using namespace std;

	ifstream fin;
	ofstream fout;

	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

	int gomi = 0;

	int a[52][52] = {};
	int check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		//		int w = 0, h = 0;
		int width = 0, height = 0;
		int count = 0;


		while (1){
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			cin >> width >> height;
			if (width == 0 && height == 0)break;

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					//初期化
					gomi = a[i + 1][j + 1] = 0;

					cin >> a[i + 1][j + 1];
				}
			}

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
//						a[i + 1][j + 1] = 1;
						count++;
//						cout << i + 1 << "  " << j + 1 << endl;
					}
				}
			}
//			cout << count << endl;
			fout << count << endl;

			//初期化
			count = 0;
			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					a[i + 1][j + 1] = 0;
				}
			}
		}
	}

	int check(int y, int x){
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (a[y + i][x + j] == 1){
					if (i == 0 && j == 0)
						j = 1;
					a[y + i][x + j] = 0;
					check(y + i, x + j);
				}
			}
		}
		return 0;
	}

/*	int check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
		return 0;
	}*/

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int W, H;
vector<vector<bool>> field;

void dfs(vector<vector<bool>>& visited, int h, int w){
    if(visited[h][w]) return;
    visited[h][w]=true;
    int dx[3]={-1, 0, 1};
    int dy[3]={-1, 0, 1};
    for(int yi=0; yi<3; ++yi){
        for(int xi=0; xi<3; ++xi){
            if(field[h+dy[yi]][w+dx[xi]]) dfs(visited, h+dy[yi], w+dx[xi]);
        }
    }
}

int solve(){
    vector<vector<bool>> visited(H+2, vector<bool>(W+2));
    int ans=0;
    for(int h=1; h<=H; ++h){
        for(int w=1; w<=W; ++w){
            if(!field[h][w]) continue;
            if(visited[h][w]) continue;
            dfs(visited, h, w);
            ++ans;
        }
    }
    return ans;
}

int main(){
    while(true){
        cin >> W >> H;
        if(W==0 && H==0) break;
        field=vector<vector<bool>>(H+2, vector<bool>(W+2));
        for(int h=1; h<=H; ++h){
            for(int w=1; w<=W; ++w){
                char c; cin >> c;
                field[h][w]=c=='1';
            }
        }
        cout << solve() << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

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

void dfs(int y, int x, vector<vector<int>> &c)
{
  c[y][x] = 0;
  for (int i = 0; i < 8; ++i)
  {
    int ny = y + dy[i], nx = x + dx[i];
    if (0 <= nx && nx < c[0].size() && 0 <= ny && ny < c.size() && c[ny][nx])
      dfs(ny, nx, c);
  }
}

int main()
{
  int w, h;
  while (cin >> w >> h, w || h)
  {
    vector<vector<int>> c;
    c.clear();
    c.shrink_to_fit();
    c.assign(h, vector<int>(w));
    for (int i = 0; i < h; ++i)
    {
      for (int j = 0; j < w; ++j)
      {
        cin >> c[i][j];
      }
    }
    int cnt = 0;
    for (int i = 0; i < h; ++i)
    {
      for (int j = 0; j < w; ++j)
      {
        if (c[i][j])
        {
          dfs(i, j, c);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

C++: #if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int W, H;
int x[8] = {1,0,-1,0,1,1,-1,-1};
int y[8] = {0,-1,0,1,1,-1,1,-1};

bool in_map(int h, int w) {
  return (1 <= h && h <= H && 1 <= w && w <= W);
}

int main() {
  while(cin >> W >> H && W > 0) {
    int M[55][55];
    for (int h = 1; h <= H; h++) {
      for (int w = 1; w <= W; w++)
        cin >> M[h][w];
    }
    queue<pair<int,int>> Q;
    int ans = 0;
    for (int sh = 1; sh <= H; sh++) {
      for (int sw = 1; sw <= W; sw++) {
        if (M[sh][sw] == 1) {
          ans++;
          Q.push(make_pair(sh,sw));
          M[sh][sw] = 0;
          while (!Q.empty()) {
            pair<int,int> p = Q.front();
            Q.pop();
            int h = p.first; int w = p.second;
            for (int i = 0; i < 8; i++) {
              int nh = h+y[i]; int nw = w+x[i];
              if (in_map(nh, nw) && M[nh][nw] == 1) {
                Q.push(make_pair(nh, nw));
                M[nh][nw] = 0;
              }
            }
          }
        }
      }
    }

    cout << ans << endl;
  }
}