Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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

#define REP(i,m,n) for(int i=(int)m; i < (int) n; ++i)
#define rep(i,n) REP(i,0,n)

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

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

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

    while(cin >> W >> H) {
        if(H == 0 && W == 0) break;
        field.assign(H, vector<int> (W, 0));
        rep(i, H) rep(j, W) cin >> field[i][j];
        int ans = 0;
        rep(i, H) rep(j, W) {
            if(field[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        cout << ans << '\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define loop(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr int LEN = 55;
bool m[LEN][LEN];

int main(){
	while(true){
		int w, h; cin >> w >> h;
		if(!w && !h) break;
		rep(i, h)rep(j, w) cin >> m[i][j];

		int ans = 0;
		rep(i, h)rep(j, w){
			if(!m[i][j]) continue;
			ans++;
			queue<P> q;
			q.push(make_pair(i, j));
			while(q.size()){
				P now = q.front(); q.pop();
				loop(di, -1, 2)loop(dj, -1, 2){
					int ni = now.first + di, nj = now.second + dj;
					if(0 <= ni && ni < h && 0 <= nj && nj < w && m[ni][nj]){
						m[ni][nj] = false;
						q.push(make_pair(ni, nj));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>

void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int w, h, i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

C++: #include <iostream>
#define MAX_W 50
#define MAX_H 50
using namespace std;

int w, h;
int t[MAX_H][MAX_W];

void PRINT() {
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      cout << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}

// 深さ優先探索
// 現在位置(y, x)
void dfs(int y, int x) {
  t[y][x] = 8;

  for(int dx = -1; dx <= 1; dx++) {
    for(int dy = -1; dy <= 1; dy++) {
      // 現在位置を(nx, ny)に更新
      int nx = x + dx;
      int ny = y + dy;

      // (nx, ny)は部屋の中か？ & t[nx][ny]は陸か？ を判定
      if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] == 1) {
        dfs(ny, nx);
        //PRINT();
      }
    }
  }
  return;
}

void solve() {
  int cnt = 0;
  for(int y = 0; y < h; ++y) {
    for(int x = 0; x < w; ++x) {

      if(t[y][x] == 1) {
        dfs(y, x);
        cnt++;
      }
      
    }
  }
  cout << cnt << endl;
}

int main() {
  while(cin >> w >> h && w && h) {

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        cin >> t[i][j];
      }
    }
    solve();
  }
  return 0;
}

C++: #define _crt_secure_no_warnings

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

vector<vector<int>>data;

int x[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int y[] = { 1, 1, 0, -1, -1, -1, 0, 1 };

void serch(int i, int j)
{
	data[i][j] = 0;
	for (size_t k = 0; k < 8; k++)
	{
		if (data[i + x[k]][j + y[k]])
		{
			serch(i + x[k], j + y[k]);
		}
	}
}

int main()
{
	int N, M;
	int ans = 0;
	while (1)
	{
		data.clear();
		ans = 0;
		cin >> N >> M;
		if (N == 0 && M == 0)break;
		swap(M, N);
		data.resize(N + 2);
		for (size_t i = 0; i < N + 2; i++)
		{
			data[i].resize(M + 2);
			for (size_t j = 0; j < M+2; j++)
			{
				data[i][j] = 0;
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				cin >> data[i][j];
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				if (data[i][j])
				{
					ans++;
					serch(i, j);
				}
			}
		}
		cout << ans<<endl;
	}
}