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;

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

void dfs(int x, int y){
	
	if (c[y][x] != 0){
		c[y][x] = 0;
		for (int i = 0; i < 8; i++){
			dfs(x + dx[i], y + dy[i]);
		}
	}

	return;
}

int main(){

	int w, h;

	while (cin >> w >> h, w, h){

		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				cin >> c[j][i];
			}
		}

		int result = 0;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0) {
					dfs(i, j);
					result++;
				}
			}
		}

		cout << result << endl;

	}

	return 0;
}

//??????????????°??£????????????
//???????????±?????????????????????WA?????????
/*
int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				cin >> c[j][i];
			}
		}

		int count = 1;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0){

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					bool buff = island[c[j][i]];

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = buff && true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}
*/


/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[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 < W && 0 <= ny && ny < H && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < W ; i++) {
      for(int j = 0 ; j < H ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	  res++;
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>

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

void land(int x,int y){
	field[x][y]=0;

	for(int yy=-1;yy<=1;yy++){
		for(int xx=-1;xx<=1;xx++){
			if(x+xx>=0&&x+xx<=w&&y+yy>=0&&y+yy<=h){
			   if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	while(1){

	scanf("%d %d",&w,&h);

	if(w==0&&h==0) break;

	kazu=0;

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

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

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

	return 0;
}

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

int H, W;
vector<vector<int>> field(H, vector<int>(W));
vector<vector<bool>> seen(H, vector<bool>(W));

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

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

int main()
{
    while (cin >> W >> H)
    {
        if (W==0 || H==0) break;
        field.resize(H);
        seen.resize(H);
        for (int h=0; h<H; h++)
        {
            field[h].resize(W);
            seen[h].resize(W);
        }
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++) 
            {
                cin >> field[h][w];
                seen[h][w]=false;
            }
        }
        int answer=0;
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++)
            {
                if (seen[h][w]==false && field[h][w]==1) dfs(h, w), answer++;
            }
        }
        cout << answer << endl;
    }
}

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

int MAP(vector<vector<int>> &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int>> map(h, vector<int>(w, 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++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}

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

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

template <class T>
T next()
{
    T x;
    cin >> x;
    return (x);
}

int w, h;
vector<vector<int>> islands;

void dfs(int ii, int jj)
{
    islands[jj][ii] = 0;
    for (int i = ii - 1; i <= ii + 1; i++)
    {
        for (int j = jj - 1; j <= jj + 1; j++)
        {
            if (i < 0 || i == w || j < 0 || j == h)
            {
            }
            else if (islands[j][i] > 0)
            {
                dfs(i, j);
            }
        }
    }
}

int main()
{
    vector<int> ans;
    while (true)
    {
        int cnt = 0;
        w = next<int>();
        h = next<int>();
        if (w + h == 0)
        {
            break;
        }
        islands.resize(h);
        rep(j, h)
        {
            islands[j].resize(w);
            rep(i, w)
            {
                cin >> islands[j][i];
            }
        }
        rep(j, h)
        {
            rep(i, w)
            {
                if (islands[j][i] > 0)
                {
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        ans.push_back(cnt);
    }
    for (int i : ans)
    {
        cout << i << endl;
    }
}