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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int w, h;
int m[50][50];

void dfs(int y, int x){
    m[y][x] = 0;
    for(int dy = -1; dy <= 1; dy++){
        for(int dx = -1; dx <= 1; dx++){
            int ny = y + dy;
            int nx = x + dx;
            if(ny >= 0 && ny < h && nx >= 0 && nx < w &&
               m[ny][nx] == 1)
                dfs(ny, nx);
        }
    }
    return;
}

int main(){
    while(1){
        cin >> w >> h;
        int ans = 0;
        if(w == 0 && h == 0) break;

        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> m[i][j];
            }
        }
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(m[i][j] == 1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int w, h, m[50][50], dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
void dfs(int x, int y)
{
	int i, nx, ny;
	m[y][x] = 0;
	for (i = 0; i < 8; i++)
	{
		if (0 <= (nx = x+dx[i]) && nx < w && 0 <= (ny = y+dy[i]) && ny < h)
			if (m[ny][nx])
				dfs(nx, ny);
	}
}
int main()
{
	int n, i, j;
	while (cin >> w >> h)
	{
		if (w == 0 && h == 0) break;
		for (i = 0; i < h; i++)
			for (j = 0; j < w; j++)
				cin >> m[i][j];
		for (i = n = 0; i < h; i++)
			for (j = 0; j < w; j++)
				if (m[i][j])
					n++, dfs(j, i);
		cout << n << endl;
	}
}

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


int w, h;
int ans;
int map[50][50];
int dx[8] = { 0, 0, 1, 1, 1, -1, -1, -1 };
int dy[8] = { 1, -1, 1, 0, -1, 1, 0, -1 };

void solve(int x, int y)
{
	map[x][y] = 0;
	for (int i = 0; i < 8; i++)
	{
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0 <= X && X < w && 0 <= Y && Y < h && map[X][Y] == 1)
		{
			solve(X, Y);
		}
	}
}

int main()
{
	while (cin >> w >> h, w + h)
	{
		ans = 0;
		for (int y = 0; y < h; y++)
		{
			for (int x = 0; x < w; x++)
			{
				cin >> map[x][y];
			}
		}

		for (int y = 0; y < h; y++)
		{
			for (int x = 0; x < w; x++)
			{
				if (map[x][y] == 1)
				{
					ans++;
					solve(x, y);
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}

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

int field[52][52];
int memo[52][52];

void dfs(int j, int i){
	if( memo[i][j] || !field[i][j] ) return;
	memo[i][j] = 1;
	dfs(j-1, i-1);
	dfs(j, i-1);
	dfs(j+1, i-1);
	dfs(j-1, i);
	dfs(j+1, i);
	dfs(j-1, i+1);
	dfs(j, i+1);
	dfs(j+1, i+1);
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				memo[i][j] = 0;
			}
		}
		int ans = 0;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] && !memo[i][j] ){
					dfs( j, i );
					++ans;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

int w,h;
int filed[51][51];

void dfs(int x,int y){
  filexd[x][y]=0;
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      int nx=x+dx,ny=y+dy;
      if(0<=nx&&nx<h&&0<=ny&&ny<w&&filed[nx][ny]==1)dfs(nx,ny);
    }
  }
}

void solve(){
  int res=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(filed[i][j]==1){
	dfs(i,j);
	res++;
      }
    }
  }
  cout<<res<<endl;
}

int main(){
  while(1){
    cin>>w>>h;
    if(w+h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>filed[i][j];
      }
    }
    solve();
  }
  return 0;
}

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

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

int w, h, c[55][55], ans;
bool is_search[55][55];
int vx[8] = {0, -1, -1, -1, 0, 1, 1, 1},
    vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};


void bfs(int x, int y) {
    queue<pair<int, int> > que;
    que.push(make_pair(x, y));
    is_search[x][y] = true;

    while (!que.empty()) {
        pair<int, int> p = que.front(); que.pop();
        //cout << p.first << " " << p.second << endl;
        rep(i,8) {
            int nx, ny;
            nx = p.first + vx[i], ny = p.second + vy[i];
            if (0 <= nx && nx < h && 0 <= ny && ny < w &&
                c[nx][ny] == 1 && is_search[nx][ny] == false) {
                   que.push(make_pair(nx, ny));
                   is_search[nx][ny] = true;
            }
        }
    }

}

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

    while (cin >> w >> h, w | h) {
        memset(c, 0, sizeof(c));
        memset(is_search, false, sizeof(is_search));
        ans = 0;
        rep(i,h) {
            rep(j,w) {
                cin >> c[i][j];
            }
        }

        rep(i,h) {
            rep(j,w) {
                if (is_search[i][j] == false && c[i][j] == 1) {
                    bfs(i,j);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

vector<int> dx = {1, 0, -1, 0, 1, 1, -1, -1}, dy = {0, 1, 0, -1, 1, -1, 1, -1};

void bfs(int i, int j, int h, int w, vector<vector<int>> &c) {
  queue<pair<int, int>> q;
  q.push(make_pair(i, j));
  c.at(i).at(j) = 0;
  
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 8; i++) {
      if (p.first + dx[i] >= 0 && p.first + dx[i] < h
       && p.second + dy[i] >= 0 && p.second + dy[i] < w) {
        if (c.at(p.first + dx[i]).at(p.second + dy[i]) == 1) {
          c.at(p.first + dx[i]).at(p.second + dy[i]) = 0;
          q.push(make_pair(p.first + dx[i], p.second + dy[i]));
        }
      }
    }
  }
  
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w + h == 0) break;
    
    vector<vector<int>> c(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c.at(i).at(j);
      }
    }
    
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (c.at(i).at(j) == 1) {
          bfs(i, j, h, w, c);
          ans++;
        }
      }
    }
    
    cout << ans << '\n';
  }
}