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>
using namespace std;
int ans;
int map[100][100];
void dfs(int x,int y,int w,int h);
int dx[8] = {1,1,0,-1,-1,-1,0,1},dy[8] = {0,1,1,1,0,-1,-1,-1};

int main(){
  int w,h;
  while(cin>>w>>h){
    ans=0;
    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){
	  map[i][j] = 0;
	  ans++;
	  dfs(i,j,w,h);
	}
      }
    // cout<<"map"<<endl;
    // for(int a=0;a<h;a++){
    //   for(int b=0;b<w;b++) cout<<map[a][b];
    //   cout<<endl;
    // }
    // cout<<endl;
    cout<<ans<<endl;
  }
  return 0;
}

void dfs(int x,int y,int w,int h){
  //cout<<"LK+LKJ"<<endl;
  int nx,ny;
  for(int i = 0;i<8;i++){
    nx = x+dx[i]; ny = y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && map[ny][nx] == 1){
      map[ny][nx] = 0;
      dfs(nx,ny,w,h);
    }
  }
}

C++: #include<iostream>
#include<stack>
#include<string.h>
using namespace std;

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

int main() {
  int w,h,t;
  while(cin>>w>>h, w||h) {
    memset(landscape, 0, sizeof(landscape));
    for(int i=1; i<=h; ++i)
      for(int j=1; j<=w; ++j)
	cin>>landscape[i][j];

    int count = 0;
    for(int i=1; i<=h; ++i) {
      for(int j=1; j<=w; ++j) {
	if(landscape[i][j] == 1) {
	  count++;
	  stack< pair<int, int> > st;
	  st.push(pair<int,int>(i,j));
	  while(!st.empty()) {
	    pair<int,int> p = st.top();
	    st.pop();
	    landscape[p.first][p.second] = 0;
	    for(int k=0; k<8; ++k)
	      if(landscape[p.first+dx[k]][p.second+dy[k]])
		st.push(pair<int,int>(p.first+dx[k], p.second+dy[k]));
	  }
	}
      }
    }

    cout<<count<<endl;
  }
}

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

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char *buf;
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		buf = new char[w*2];
		cin.getline(buf, sizeof(char)*w*2);
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
		delete[] buf;
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		int newx = x+periphe[i][0],
			newy = y+periphe[i][1];
		if(newx < 0 || w-1 < newx ||
			newy < 0 || h-1 < newy) continue;
		if(island[newx][newy]){
			if(!check[newx][newy])
				howBigIslands(island, check,
				newx, newy, w, h);
		}
	}
}

C++: //
//  main.cpp
//  AOJ-1160
//
//  Created by Kenta Kodera on 2017/06/18.
//  Copyright ?? 2017??´ Kenta Kodera. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
#include <cassert>
using namespace std;

int W, H;

void dfs(vector<vector<int>> &field, int h, int w){
  int dhw[3] = {1, -1, 0};
  field[h][w] = 0;
  
  for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
      int nh = h+dhw[i];
      int nw = w+dhw[j];
      for(int h=0; h<H; h++){
        for(int w=0; w<W; w++){
          if(0<=nh && nh<H && 0<=nw && nw<W && field[nh][nw]==1){
            dfs(field, nh, nw);
          }
        }
      }
    }
  }
  
}


int main(){
  
  while(cin >> W >> H, W|H){
    vector<vector<int>> field(H);
    for(int h=0; h<H; h++){
      field[h].resize(W);
    }
    
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        cin >> field[h][w];
      }
    }
    
    int n=0;
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        if(field[h][w] == 1){
          dfs(field, h, w);
          n++;
        }
      }
    }
    cout << n << endl;
    
  }
  

  return 0;
}

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

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

int dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= w || y >= h) return 0;
  if (c[y][x] == 0) return 0;
  c[y][x] = 0;
  for (int i = 0; i < 8; i++) dfs(x + dx[i], y + dy[i]);
  return 1;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0) break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) scanf("%d", &c[i][j]);
    int counter = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) counter += dfs(j, i);
    printf("%d\n", counter);
  }
}