Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

using namespace std;
typedef long long ll;

int ans = 0;
vector <vector<int> > MAP;

int solve(int locateX,int locateY,int w,int h){
  //!
  if(locateX<0 || locateY<0 || locateX>=h || locateY>=w)return 0;
  //CHECK
  if(MAP[locateX][locateY]==1){
    MAP[locateX][locateY]=0;
    //!--
    solve(locateX,locateY+1,w,h);
    solve(locateX+1,locateY+1,w,h);
    solve(locateX+1,locateY,w,h);
    solve(locateX+1,locateY-1,w,h);
    solve(locateX,locateY-1,w,h);
    solve(locateX-1,locateY-1,w,h);
    solve(locateX-1,locateY,w,h);
    solve(locateX-1,locateY+1,w,h);
    //--!
    return 1;
  }
  return 0;
}

int ope(){
  int w,h;
  ans=0;
  MAP.resize(0);
  cin >> w >> h;
  if(!w&&!h)return 0;
  //INPUT
  for(int i=0;i<h;++i){
    vector<int> temp;
    for(int j=0;j<w;++j){
      int ti;
      cin >> ti;
      temp.push_back(ti);
    }
    MAP.push_back(temp);
  }
  //EXEC
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j)  
      ans+=solve(i,j,w,h);

  //OUTPUT
  cout << ans << endl;
  return 1;
}
int main(){
  while(ope()){}
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

using Graph = vector<vector<int>>;
//using Graph = vector<vector<Edge>>;
bool seen[100][100];
int field[100][100];

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

int w, h;

void dfs(int i, int j){
    seen[i][j] = true;
    REP(k, 8){
        if(i+dx[k] >= h || i+dx[k] < 0 || j+dy[k] >= w || j+dy[k] < 0) continue;
        if(field[i+dx[k]][j+dy[k]] == 0) continue;
        if(seen[i+dx[k]][j+dy[k]]) continue;
        dfs(i+dx[k], j+dy[k]);
    }
}

int main()
{
    for(;;){
        cin >> w >> h;
        if(w==0 && h==0)
            break;
        REP(i, h)
            REP(j, w){
                cin >> field[i][j];
                seen[i][j] = false;
            }
        int count = 0;
        REP(i, h)
            REP(j, w){
                if(seen[i][j] || field[i][j] == 0) continue;
                dfs(i, j);
                count++;
            }
        cout << count << endl;
    }

    return 0;
    
}

C++: #include <iostream>
#include <queue>
#include <utility>

using namespace std;

using Point = pair<int,int>;

int map[50][50];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;
    map[y][x] = 0;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
        }
      }
    }
  }

}

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

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

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

using namespace std;

int field[52][52];

void DFS(int x, int y){
    if(field[x][y]==1){
        field[x][y]=-1;
        DFS(x-1,y-1);
        DFS(x,y-1);
        DFS(x+1,y-1);
        DFS(x-1,y);
        DFS(x+1,y);
        DFS(x-1,y+1);
        DFS(x,y+1);
        DFS(x+1,y+1);
    }
}


int main(){
    int w,h;
    while(cin>>w>>h){
        if(w==0&&h==0)break;
        for(int i=0; i<50; i++){
            for(int j=0; j<50; j++){
                field[i][j]=0;
            }
        }
        
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                cin>>field[i][j];
            }
        }
        int sum=0;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                if(field[i][j]==1){
                    sum++;
                    DFS(i,j);
                }
            }
        }
        cout<<sum<<endl;
    }
}

C++: ///
// File:  1160.cpp
// Author: ymiyamoto
//
// Created on Sun Nov 12 16:15:22 2017
//

#include <array>
#include <cstdint>
#include <iostream>

using namespace std;

void dfs(int32_t h, int32_t w, array<array<uint32_t, 50>, 50> &arr, int32_t i, int32_t j)
{
  arr[i][j] = 0;

  for (int32_t l = -1; l <= 1; l++) {
    for (int32_t k = -1; k <= 1; k++) {
      if (0 <= i + l && i + l < h && 0 <= j + k && j + k < w) {
        if (arr[i + l][k + j] == 1) {
          dfs(h, w, arr, i + l, k + j);
        }
      }
    }
  }
}

int32_t main()
{
  while (true) {
    uint32_t w, h;
    cin >> w >> h;
    if (w == 0) break;

    array<array<uint32_t, 50>, 50> arr;
    for (uint32_t i = 0; i < h; i++) {
      for (uint32_t j = 0; j < w; j++) {
        cin >> arr[i][j];
      }
    }

    uint32_t count = 0;
    for (uint32_t i = 0; i < h; i++) {
      for (uint32_t j = 0; j < w; j++) {
        if (arr[i][j] == 1) {
          count++;
          dfs(h, w, arr, i, j);
        }
      }
    }
    cout << count << endl;
  }

  return 0;
}