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 <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
int c[50][50];
bool visited[50][50];
int vy[8] = {-1,-1,0,1,1,1,0,-1};
int vx[8] = {0,1,1,1,0,-1,-1,-1};
void calc(int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return;
  if(visited[y][x] || c[y][x] == 0) return;
  visited[y][x] = true;
  FOR(i,0,8) calc(y+vy[i],x+vx[i]);
  return;
}
int main()
{
  while(cin >> W >> H, (W || H)) {
    CLR(visited);
    FOR(y,0,H) FOR(x,0,W) cin >> c[y][x];
    int ans = 0;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(visited[y][x] || c[y][x] == 0) continue;
        calc(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int w, h;
bool state[51][51];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

void dfs(int x, int y){
    state[x][y] = 0;
    
    for(int i = 0; i < 8; i++){
        int nx = x + dx[i], ny = y + dy[i];
        if(isRegion(nx, ny) && state[nx][ny]){
            dfs(nx, ny);
        }
    }
}

int main(int argc, const char * argv[]) {
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                cin >> state[x][y];
            }
        }
        
        int cnt = 0;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                if(state[x][y]){
                    dfs(x, y);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
}

C++: #include <cstdio>
#include <stack>
#include <utility>

using namespace std;

typedef pair<int, int> point;

int w, h;
int c[50][50];
const int d[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int count()
{
    stack<point> s;
    bool v[50][50] = {};
    int n = 0;

    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (v[x][y] || c[x][y] == 0)
                continue;

            s.push(point(x, y));
            v[x][y] = true;
            ++n;
            while (!s.empty()) {
                int x = s.top().first, y = s.top().second;
                s.pop();

                for (int i = 0; i < 8; ++i) {
                    int xx = x + d[i][0], yy = y + d[i][1];
                    if (0 <= xx && xx < w && 0 <= yy && yy < h
                            && !v[xx][yy] && c[xx][yy] == 1) {
                        s.push(point(xx, yy));
                        v[xx][yy] = true;
                    }
                }
            }
        }
    }

    return n;
}

int main()
{
    for (;;) {
        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0)
            break;

        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j)
                scanf("%d", &c[j][i]);

        printf("%d\n", count());
    }

    return 0;
}

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


//AOJ How many islands?
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int dx[8] = {1,-1,0,0,1,1,-1,-1};
int dy[8] = {0,0,1,-1,1,-1,1,-1};
int h,w;
using Graph = vector<vector<int>>;
vector<vector<bool>> seen;
vector<vector<int>> maze;

void dfs(P p){
    seen[p.first][p.second] = true;

    for(int i = 0;i < 8;i++){
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
        if(seen[ny][nx]) continue;
        if(maze[ny][nx] == 0) continue;

        dfs(make_pair(ny,nx));
    }
}

void recur(int w,int h) {
    int count = 0;
    maze.assign(h,vector<int>(w,0));
    seen.assign(h,vector<bool>(w,false));
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            cin >> maze[i][j];
        }
    }

    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(maze[i][j]==0) continue;
            if(seen[i][j]) continue;
            dfs(make_pair(i,j));
            count++;
        }
    }
    
    cout << count << endl;
    w = 0;
    h = 0;
}

int main(){
    while(true){
        cin >> w >> h;
        if(w*w + h*h == 0) break;
        recur(w,h);
    }
    return 0;
}



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


// //AOJ How many islands?
// //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

// int dx[8] = {1,-1,0,0,1,1,-1,-1};
// int dy[8] = {0,0,1,-1,1,-1,1,-1};
// int h,w;
// using Graph = vector<vector<int>>;
// vector<vector<bool>> seen;
// vector<vector<int>> maze;

// void dfs(P p){
//     seen[p.first][p.second] = true;

//     for(int i = 0;i < 8;i++){
//         int nx = p.second + dx[i];
//         int ny = p.first + dy[i];
//         if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
//         if(seen[ny][nx]) continue;
//         if(maze[ny][nx] == 0) continue;

//         dfs(make_pair(ny,nx));
//     }
// }

// int main() {
//     cin >> w >> h;
//     int count = 0;
//     maze.assign(h,vector<int>(w,0));
//     seen.assign(h,vector<bool>(w,false));
//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             cin >> maze[i][j];
//         }
//     }

//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             if(maze[i][j]==0) continue;
//             if(seen[i][j]) continue;
//             dfs(make_pair(i,j));
//             count++;
//         }
//     }
    
//     cout << count << endl;
// }

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};

int w,h,ans=0;
bool c[55][55];

void bfs(int y,int x)
{
    c[y][x]=0;
    REP(i,8) 
    {
        int ny=y+dy[i];int nx=x+dx[i];
        if(c[ny][nx]&&ny>=0&&ny<h&&nx>=0&&nx<w)bfs(ny,nx);
    }
}

int main()
{

    while(cin>>w>>h,w>0)
    {
        REP(i,h)REP(j,w)cin>>c[i][j];
        ans=0;c[55][55]={};
        REP(i,h)REP(j,w)if(c[i][j]){bfs(i,j);ans++;}
        cout<<ans<<endl;
    }
}