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<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#include<string.h>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
using namespace std;

int H,W;
vector<vector<int>>field;

void dfs(int h,int w) {
    field[h][w] = 0;
    for(int dh = -1;dh <= 1;dh++) {
        for(int dw = -1;dw <= 1;dw++) {
            int nh = h + dh, nw = w + dw;
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if(field[nh][nw] == 0) continue;
            
            dfs(nh,nw);
        }
    }
}

int main() {
    while(true){
        cin >> W >> H;
        if(W == 0 || H == 0) break;
        field.assign(H, vector<int>(W, 0));
        for(int h = 0; h < H;h++) {
            for(int w = 0;w < W;w++) {
             cin >> field[h][w];
            }
          }
        int count = 0;
        for(int h = 0;h < H;h++) {
            for(int w = 0; w < W;w++) {
                if(field[h][w] == 0) continue;
                dfs(h,w);
                count++;
            }
        }
    cout << count << endl;
    }
    return 0;
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+1, y);
    dfs(x+1, y+2);
    dfs(x+1, y-2);
    dfs(x-1, y);
    dfs(x-1, y+2);
    dfs(x-1, y-2);
    dfs(x, y+2);
    dfs(x, y-2);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            cin.getline(c[k], sizeof(c[k]));
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < 2 * W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

const int dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dh[] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int>> to;
vector<vector<int>> seen;

int W, H;

void dfs(int a, int b) {
    seen[a][b] = 1;
    rep(i, 8) {
        int nh = a + dh[i], nw = b + dw[i];
        if (nh < 0 || H <= nh || nw < 0 || W <= nw) continue;
        if (seen[nh][nw]) continue;
        if (to[nh][nw] == 0) continue;
        dfs(nh, nw);
    }
}

void solve(int w, int h) {
    to = vector<vector<int>>(h, vector<int>(w));
    seen = vector<vector<int>>(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> to[i][j];
    int ans = 0;
    rep(i, h) rep(j, w) if ((!seen[i][j]) && (to[i][j] == 1)) {
        dfs(i, j);
        ans++;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        solve(W, H);
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
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();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

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

  int w, h;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    int mp[55][55];
    bool used[55][55];

    queue<pair<int, int>> que;
    rep(y, h) {
      rep(x, w) {
        cin >> mp[y][x];
        if (mp[y][x] == 0)
          used[y][x] = false;
        else
          used[y][x] = true;
      }
    }

    int ans = 0;

    rep(y, h) {
      rep(x, w) {
        if (used[y][x]) {
          ans++;
          que.push(make_pair(x, y));
          while (!que.empty()) {
            pair<int, int> p = que.front();
            que.pop();

            rep(i, 8) {
              int nx = p.first + ddx[i];
              int ny = p.second + ddy[i];
              if (0 <= nx && nx < w && 0 <= ny && ny < h && used[ny][nx]) {
                used[ny][nx] = false;
                que.push(make_pair(nx, ny));
              }
            }
          }
        }
      }
    }

    cout << ans << "\n";
  }

  return 0;
}