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 <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

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

void dfs(int y, int x, int h, int w){
    c[y][x] = '0';
    for(int i = 0; i < 8; i++){
        int ny = y + dy[i], nx = x + dx[i];
        if(ny < 0 or h <= ny or nx < 0 or w <= nx or c[ny][nx] == '0') continue;
        dfs(ny, nx, h, w);
    }
}

int main(void){
    while(true){
        int w, h;
        cin >> w >> h;
        if(w == 0 and h == 0) break;

        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                cin >> c[y][x];
            }
        }

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

                dfs(y, x, h, w);
                res++;
            }
        }

        cout << res << '\n';
    }

    return 0;
}

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

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

short map[50][50];
int w, h;

void dfs(int x, int y) {
    map[x][y] = 0;
    int d[3] = {-1,0,1};
    rep(i, 9) {
        int xx = x+d[i%3];
        int yy = y+d[i/3];
        if( 0<=xx && xx<w && 0<=y && yy<h && map[xx][yy]==1 ) {
            dfs( xx, yy );
        }
    }
}

int main(){
    while( cin >> w >> h, w||h ) {
        int lands = 0;
        rep(y,h) rep(x,w) cin >> map[x][y];
        rep(y, h) {
            rep(x, w) {
                if( map[x][y] == 1 ) {
                    lands++;
                    dfs(x, y);
                }
            }
        }
        cout << lands << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int cnt;
int w,h;
char map[52][52];
void func(int y,int x)
{
    map[y][x]='0';
    int nx,ny;
    for(int dy=-1;dy<=1;dy++)
    {
        for(int dx=-1;dx<=1;dx++)
        {
            ny=y+dy;
            nx=x+dx;
            if(map[ny][nx]=='1'&&0<=ny&&0<=nx&&ny<h&&nx<w)
            {
                func(ny,nx);
            }
        }
    }
    return;
}
int main()
{
    while(cin>>w>>h,w)
    {
        cnt=0;
        for(int i=0;i<52;i++)
        {
            for(int j=0;j<52;j++)
            {
                map[i][j]='0';
            }
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                cin>>map[i][j];
            }
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(map[i][j]=='1')
                {
                    func(i,j);
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<climits>
#include<ctime>
#include<cstring>
#define REP(i,p,n) for(int i=p;i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl

using namespace std;

int W,H;
int sima_cnt;
int sima_size;
int sima[51][51];
bool is_visit[51][51];

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

void saiki(int w, int h) {
	int aw=0,ah=0;
	rep(i,8) {
		aw = w + dw[i];
		ah = h + dh[i];
		if ((0<=aw && aw<W) && (0<=ah && ah<H)) {
			//次の島を訊ねる条件
			if (sima[aw][ah] && !is_visit[aw][ah]) { 
				is_visit[aw][ah] = true;
				sima_size++;
				saiki(aw,ah);
			}
			else { 
				is_visit[aw][ah] = true;
			}
		}
	}
}

int main() {
	while(cin >> W >> H) {
		if (W==0 && H==0) { break; }
		sima_cnt = 0;
		sima_size = 0;
		rep(h,51) {
			rep(w,51) {
				sima[w][h]=0;
				is_visit[w][h]=0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w][h];
			}
		}

		rep(h,H) {
			rep(w,W) {
				sima_size = 0;
				if (sima[w][h] && !is_visit[w][h]) { sima_size++; }
				saiki(w,h);
				if (sima_size>0) {
					sima_cnt++;
				}
			}
		}
		cout << sima_cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w, h, c[50][50], ans = 0;
void solve(int p, int q) {
	c[p][q] = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (p + i >= 0 && p + i < h && q + j >= 0 && q + j < w) {
				if (c[p + i][q + j]) {
					solve(p + i, q + j);
				}
			}
		}
	}
}
int main() {
	while (cin >> w >> h && w + h) {
		ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[i][j]) {
					//cout << i << " " << j << endl;
					ans++;
					solve(i, j);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

#define ALL(x) (x).begin(),(x).end()
#define IN(type,a) type a;cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl
int w,h;
void dfs(int y, int x, vector<vector<int>>& a, vector<vector<bool>>& check){
    if ((x<0)||(y<0)||(y>=h)||(x>=w)) return;
    if (a[y][x]==0) return;
    if (check[y][x]) return;
    check[y][x] = true;
    for (int i = -1; i<2; i++) {
        for (int j = -1; j<2; j++) {
                dfs(y+i, x+j, a, check);
        }
    }
    
}
int main() {
    while (true) {
        cin >> w >> h;
        if (w==0) {
            break;
        }
        vector<vector<int>> a(h,vector<int>(w));
        vector<vector<bool>> check(h,vector<bool>(w,false));
        int count = 0;
        REP(i, h){
            REP(j, w){
                cin >> a[i][j];
            }
        }
        
        REP(i, h){
            REP(j, w){
                if ((a[i][j]==1)&&(!check[i][j])) {
                    count++;
                    dfs(i, j, a, check);
                }
            }
        }
        COUT(count);
    }

}