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>

using namespace std;

int w, h, cnt;
int island[51][51];
int dx[] = {1, 1, 0, -1, -1, -1,  0,  1};
int dy[] = {0, 1, 1,  1,  0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || ny < 0 || w <= nx || h <= ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

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

    for(int i = 0 ; i < h ; i++)
      for(int j = 0 ; j < w ; j++)
	cin >> island[i][j];

    int cnt = 0;
    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
	if(island[i][j]){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int islands=0;
int w,h;
int field[50][50];
void bfs(int, int);

int main(){

  while(cin>>w>>h&&w!=0&&h!=0){

    for(int i=0;i<50;i++){
      for(int j=0;j<50;j++){
        field[i][j] = -1;
      }
    }

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        cin >> field[i][j];
        //        cout << field[i][j] << " ";
      }
      //    cout << endl;
    }
    // cout << endl;
    int ans=0; 

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        if(field[i][j]==1){
          field[i][j]=0;
          bfs(i,j);
          ans++;
        }
      }
    }
    /*   if(w==5&&h==4){
         for(int i=0;i<w;i++){
         for(int j=0;j<h;j++){
         cout << field[i][j] << " ";
         }
         cout << endl;
         }
         }*/
    cout << ans << endl;
  }

  return 0;
}

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

C++: //
//AOJ vol11-1160
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
//20170616
//640009E
//Yang Bo
//

#include <iostream>

using namespace std;

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

int recursion(int y, int x)
{
  int number = 1;
  a[y][x] = 0;

  if (x < w - 1)
    if (a[y][x + 1] == 1)
      number = recursion(y, x + 1);
  if (y < h - 1)
    if (a[y + 1][x] == 1)
      number = recursion(y + 1, x);
  if (x > 0)
    if (a[y][x - 1] == 1)
      number = recursion(y, x - 1);
  if (y > 0)
    if (a[y - 1][x] == 1)
      number = recursion(y - 1, x);
  if (y < h - 1 || x < w - 1)
    if (a[y + 1][x + 1] == 1)
      number = recursion(y + 1, x + 1);
  if (y < h - 1 || x > 0)
    if (a[y + 1][x - 1] == 1)
      number = recursion(y + 1, x - 1);
  if (y > 0 || x < w - 1)
    if (a[y - 1][x + 1] == 1)
      number = recursion(y - 1, x + 1);
  if (y > 0 || x > 0)
    if (a[y - 1][x - 1] == 1)
      number = recursion(y - 1, x - 1);
  

  //cout << "recursion(" << y << ", "<< x << ") = " << number << endl;
  return number;
}

int main()
{
  while (cin >> w >> h) {
    if (w == 0)
      break;
    else {
      for (int i = 0; i < h; i++) {
	for (int j = 0; j < w; j++) {
	  cin >> a[i][j]; //??\???
	}
      }
      int ans = 0;
      for (int i = 0; i < h; i++) {
	for (int j = 0; j < w; j++) {
	  if (a[i][j] == 1) {
	    //cout << "i = " << i << " ,j = " << j << endl;
            ans += recursion(i, j);
	  }
	}
      }
      cout << ans << endl;
    }
  }
}

/*
??¢?????´??????????????????????????????????????????????´°?????????????°???\?????????????£???????????????£???????????£??????
*/

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

void dfs(int x, int y) {
	if(seamap[x][y]) {
		seamap[x][y] = 0;

		if(seamap[x-1][y-1]) {
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, w) {
		FOR(i, 0, 52) {
			FOR(j, 0, 52) {
				seamap[i][j] = 0;
			}
		}

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				cin >> seamap[i][j];
			}
		}

		num = 0;

		FOR(i, 1, 51) {
			FOR(j, 1, 51) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;;

int c[100][100],w,h;

void dfs(int a, int b) {
	c[a][b] = 0;
	for (int k = -1; k <= 1; k++) {
		for (int l = -1; l <= 1; l++) {
				if (a + k >= 0 && a + k < h&&b + l >= 0 && b + l < w) {
					if (c[a + k][b + l] == 1){
					dfs(a + k, b + l);
				}
			}
		}
	}
	return;
}

int main() {
	while (1) {
		int ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		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] == 1) {
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}