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 <bits/stdc++.h>

using namespace std;
using ll = long long int;

vector<pair<int, int>> dirs = {
    {-1, -1}, {-1, -0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, -0}, {1, 1},
};

void dfs(vector<vector<int>> &field, vector<vector<bool>> &visit, int x, int y,
         function<bool(int, int)> check) {
  visit[y][x] = true;

  for (const auto &dir : dirs) {
    int nx = x + dir.first;
    int ny = y + dir.second;
    if (check(nx, ny))
      dfs(field, visit, nx, ny, check);
  }
}

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

  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;
    vector<vector<int>> field(h, vector<int>(w));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> field[y][x];
      }
    }
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    auto check = [&](int x, int y) {
      return (0 <= x && x < w) && (0 <= y && y < h) && field[y][x] == 1 &&
             !visit[y][x];
    };
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (visit[y][x] || field[y][x] == 0)
          continue;
        ans++;
        dfs(field, visit, x, y, check);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

vector<vector<int>> c(50, vector<int>(50));

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

vector<vector<bool>> seen(50, vector<bool>(50));
void dfs(vector<vector<int>> &c, int sy, int sx, int h, int w) {
    seen[sy][sx] = true;
    for(int i = 0; i < 8; i++){
        int ey = sy + dy[i], ex = sx + dx[i]; 
        if(ey < 0 || ey >= h || ex < 0 || ex >= w) continue;
        if(c[ey][ex] == 0 || seen[ey][ex]) continue;
        dfs(c, ey, ex, h, w);
    }
}

int main(){
    int w,  h;
    vector<int> cnt;
    int check = -1;
    cin >> w >> h;
    while (w != 0 || h != 0)
    {
        for(int i = 0 ; i < h; i++){
            for (int j = 0; j < w; j++)
            {
               cin >> c[i][j];
               seen[i][j] = false;
            }
        }
        cnt.push_back(0);
        check++;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if(!seen[i][j] && c[i][j] == 1){
                    dfs(c, i, j, h, w);
                    cnt[check]++;
                }
            }
        }
        cin >> w >> h;
    }
    for (int i = 0; i < cnt.size(); i++)
    {
        cout << cnt[i] << endl;
    }
    
}

C++: #include<iostream>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
const int WMAX = 100, HMAX = 100;
int w, h;
int island[WMAX][HMAX];

void dfs(int x, int y) {
  if (x < 0 or w <= x) return;
  if (y < 0 or h <= y) return;
//  cout << "(" << x << " " << y << ")" << endl;
  if (!island[x][y]) return;
  island[x][y] = false;
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
      dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  REP(dir, 8) {
    dfs(x + dx[dir], y + dy[dir]);
  }
}

int solve() {
  int ans = 0;
  REP(y, h) {
    REP(x, w) {
      if (island[x][y]) {
        ans++;
        dfs(x, y);
      }
    }
  }
  return ans;
}

int main() {
  while (cin >> w >> h and w) {
    REP(y, h) {
      REP(x, w) {
        int c;
        cin >> c;
        island[x][y] = c;
      }
    }

    cout << solve() << endl;
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

void dfs(int x, int y) {
	if(seamap[x-1][y-1]) {
		seamap[x-1][y-1] = 0;
		dfs(x-1, y-1);
	}
	else if(seamap[x][y-1]) {
		seamap[x][y-1] = 0;
		dfs(x, y-1);
	}
	else if(seamap[x+1][y-1]) {
		seamap[x+1][y-1] = 0;
		dfs(x+1, y-1);
	}
	else if(seamap[x-1][y]) {
		seamap[x-1][y] = 0;
		dfs(x-1, y);
	}
	else if(seamap[x+1][y]) {
		seamap[x+1][y] = 0;
		dfs(x+1, y);
	}
	else if(seamap[x-1][y+1]) {
		seamap[x-1][y+1] = 0;
		dfs(x-1, y+1);
	}
	else if(seamap[x][y+1]) {
		seamap[x][y+1] = 0;
		dfs(x, y+1);
	}
	else if(seamap[x+1][y+1]) {
		seamap[x+1][y+1] = 0;
		dfs(x+1, y+1);
	}
	else {
		num++;
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		FOR(i, 1, h+1) {
			seamap[i][w+1] = 0;
		}

		FOR(j, 1, w+1) {
			seamap[h+1][j] = 0;
		}
	
		num = 0;

		dfs(1, 1);

		cout << num << endl;
	}
}