Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <string>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1, 1, -1,-1,1};
int dy[] = {1, 0, -1, 0, 1, 1,-1,-1};
#define INF 1000000000000
#define MAX 200001
#define MOD 1000000007
ll fac[MAX], finv[MAX], inv[MAX];
const int MX = 1000005;
vector<int> d(100,0);
vector<int> f(100,0);

void dfs(int x,int y,Graph &G,Graph &visited){
    visited[y][x]=1;
    for(int A=0;A<8;A++){
        int n_x=x + dx[A];
        int n_y=y + dy[A];
        if(n_x<0 or 49<n_x or n_y<0 or 49<n_y) continue;
        if(G[n_y][n_x]==1&&visited[n_y][n_x]==0){
            dfs(n_x,n_y,G,visited);
        }
    }
}

int main(){
    vector<int> res;
    while(true){
        vector<vector<int>> visited(50,vector<int>(50,0));
        int w,h;cin>>w>>h;
        int ans=0;
        if(w==0&&h==0) break;
        vector<vector<int>> G(50,vector<int>(50,0));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin>>G[i][j];
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(G[i][j]==1&&visited[i][j]==0){
                    dfs(j,i,G,visited);
                    ans++;
                }
            }
        }
        res.push_back(ans);
    }
    int s=res.size();
    for(int i=0;i<s;i++) cout<<res[i]<<endl;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

struct node{
  int x,y;
};

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

int main(){
  int w,h,i,j,k,ans=0;
  while(1){
    queue<node> que;
    scanf("%d %d",&w, &h);
    if(w+h == 0)break;
    int c[h][w];
    ans = 0;
    node now,next,to;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)scanf("%d",&c[i][j]);
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]){
          ++ans;
          now.x = j; now.y = i;
          que.push(now);
          while(!que.empty()){
            next = que.front();
            c[next.y][next.x] = 0;
            que.pop();
            for(k=0;k<8;k++){
              to.x = next.x + dx[k];
              to.y = next.y + dy[k];
              if(to.x<0 || to.x >=w || to.y<0 || to.y >=h || c[to.y][to.x]==0)continue;
              que.push(to);
            }
          }
        }
      }
    }
    printf("%d",ans);
  }
  return 0;
}

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

using namespace std;


int read_map(vector< vector <int> >& map,int &w, int &h){
  cin >> w >>  h;
  if (w == 0 && h == 0){
    return 0;
  }
  map.reserve(h);
  for (int i = 0; i < h ; ++i) map[i].resize(w);
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      cin >> map[i][j];
    }
  }
  return 1;
}


bool island_check(vector<vector<int> >& map, int i, int j){
  if (map[i][j] == 1){
    return true;
  }
  return false;
}

void walk_island(vector<vector<int> >& map, int i, int j, int w, int h){
  int start_i, start_j;
  start_i = i;
  start_j = j;
  while (++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  j = start_j;
  while (--j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  while (++i < h){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  while (--i >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (++i < h && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && ++j < w){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  i = start_i;
  j = start_j;
  while (--i >= 0 && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
  while (++i < h && --j >= 0){
    if (island_check(map, i, j))
    {
      map[i][j] = 2;
      walk_island(map, i, j, w, h);
    }else{
      break;
    }
  }
}

int search_island_num(vector< vector <int> >& map, int w, int h){
  int island_count = 0;
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      if(map[i][j] == 1){
        island_count++ ;
        map[i][j] = 2;
        walk_island(map, i, j, w, h);
      }
    }
  }
  return island_count;
}

int main(){
  vector< vector<int> > map;
  int w, h;
  while (read_map(map, w, h) != 0) {
    cout << search_island_num(map, w, h) << endl;
    map.clear();
  }
}