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 <queue>
using namespace std;

int main() {

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

  long long int w, h;

  while( true ) {

    cin >> w >> h;

    if ( w == 0 ) break;

    int map[52][52] = {0};

    for ( int y = 0; y < h; y++ ) {
      for ( int x = 0; x < w; x++ ) {

	cin >> map[x+1][y+1];

      }
    }

    int ans = 0;

    while( true ) {

      bool flag = false;
      for ( int y = 1; y < h + 1; y++ ) {
	for ( int x = 1; x < w + 1; x++ ) {

	  if ( map[x][y] == 1 ) {
	    queue<int> qx, qy;
	    qx.push(x);
	    qy.push(y);
	    map[x][y] = 0;
	    while( qx.size() > 0 ) {

	      int lx = qx.front();
	      int ly = qy.front();
	      for ( int i = 0; i < 8; i++ ) {
		if ( map[lx+dx[i]][ly+dy[i]] == 1 ) {
		  map[lx+dx[i]][ly+dy[i]] = 0;
		  qx.push(lx+dx[i]);
		  qy.push(ly+dy[i]);
		}
	      }
	      qx.pop();
	      qy.pop();

	    }
	    flag = true;
	    break;
	  }

	}
	if ( flag ) break;
      }
      if ( flag == false ) break;

      ans++;

    }

    cout << ans << endl;

  }

  return 0;

}

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

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

void solve(int x,int y){
	if(map[y][x] == 1){
		map[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		if(w==0&&h==0){
			break;
		}

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

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

		cout << count << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>

using namespace std;

class Solver {
public:
	static void solve() {
		int w, h;
		cin >> w >> h;
		vector<int> res;
		while (w > 0 && h > 0) {
			vector<vector<bool>> grid(h, vector<bool>(w));
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					int now;
					cin >> now;
					grid[i][j] = now;
				}
			}

			int cnt = 0;
			vector<vector<int>> visited(h, vector<int>(w, false));

			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!grid[i][j] || visited[i][j])continue;

					cnt++;

					queue<tuple<int, int>> que;
					que.push(make_tuple(i, j));
					while (!que.empty()) {
						auto pos = que.front();
						que.pop();
						int y = get<0>(pos);
						int x = get<1>(pos);
						if (y < 0 || h <= y || x < 0 || w <= x
							|| !grid[y][x] || visited[y][x])continue;

						visited[y][x] = true;

						que.push(make_tuple(y - 1, x));
						que.push(make_tuple(y + 1, x));
						que.push(make_tuple(y, x - 1));
						que.push(make_tuple(y, x + 1));
						que.push(make_tuple(y - 1, x - 1));
						que.push(make_tuple(y + 1, x - 1));
						que.push(make_tuple(y - 1, x + 1));
						que.push(make_tuple(y + 1, x + 1));
					}
				}
			}

			res.emplace_back(cnt);

			cin >> w >> h;
		}

		for (int i = 0; i < res.size(); i++) {
			cout << res[i] << endl;
		}
	}
};

int main() {
	Solver::solve();
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <set>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <numeric>
#include <sstream>
#include <list>
#include <map>
#include <functional>
#include <iterator>

#define rep(i,p,n) for(int i=(int)p;i<(int)n;i++)
#define repp(i,p,n) for(int i=(int)p;i>=(int)n;i--)
#define Sort(x) sort(x.begin(),x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
using namespace std;

vector<vector<int>> v;
int w, h;

void dfs(int y, int x) {
	if (v[y][x] == 1)v[y][x] = 0;

	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int nx = x + i, ny = y + j;

			if (!(nx < 0 || ny < 0 || nx >= w||ny >= h)&&v[ny][nx] == 1) dfs(ny, nx);
		}
	}
	
	return;
}

int main(){
	while (1) {
		int ans=0;
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		v.resize(h, vector<int>(w));
		rep(i, 0, h) {
			rep(j, 0, w) {
				cin >> v[i][j];
			}
		}

		rep(i, 0, h) {
			rep(j, 0, w) {
				if (v[i][j] == 1) {
					ans++;
					dfs(i, j);
				}
			}
		}
		cout << ans << endl;
		v.clear();
	}
	
	return 0;
}

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

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

void dfs(const int x, const int y, const int label) {
	field[y][x] = label;
	for(int i = 0; i < 8; ++i) {
		const int nx = x + dx[i], ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= w || ny >= h)
			continue;

		if(field[ny][nx] == 1)
			dfs(nx, ny, label);
	}
}

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

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

		int label = 2;
		for(int i = 0; i < h; ++i)
			for(int j = 0; j < w; ++j)
				if(field[i][j] == 1)
					dfs(j, i, label++);

		cout << label - 2 << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include<bits/stdc++.h>
using namespace std;

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

void dfs(int v[][55], int x, int y){
    v[y][x] = 0;
    for(int i=0;i<8;++i)
        if(v[y+dy[i]][x+dx[i]] == 1)
            dfs(v, x+dx[i], y+dy[i]);
}

int main(){
    int w, h;
    while(cin >> w >> h, w != 0){
        int v[55][55]={0};
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j)
                cin >> v[i][j];
        int ans = 0;
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j)
                if(v[i][j] == 1){
                    ans++;
                    dfs(v, j, i);
                }
        cout << ans << endl;
    }

    return 0;
}