Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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

using namespace std;
char Field[52][52];
int w, h;

void DFS(int y, int x)
{
	Field[y][x] = 0;
	for(int i = -1; i <= 1; ++i)
	{
		for(int j = -1; j <= 1; ++j)
		{
			int X = x + j;
			int Y = y + i;
			if((0 < X && X <= w) && (0 < Y && Y <= h) && Field[Y][X] == '1')
			{
				DFS(Y, X);
			}
		}
	}
}

void solve()
{
	while(cin >> w >> h, w || h)
	{
		memset(Field, 0, sizeof(Field));
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				cin >> Field[i][j];
			}
		}

		int count = 0;
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				if(Field[i][j] == '1')
				{
					count++;
					DFS(i, j);
				}
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
struct Graph{
  using ver_t=int32_t; using edge_t=pair<ver_t,ver_t>;
  vector<vector<ver_t>> field;
  const edge_t dr[8]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
  void dfs(edge_t frm){
    // preorder
    field[frm.first][frm.second]=0;
    for(const auto& v:dr){
      auto x=frm.first+v.first; auto y=frm.second+v.second;
      if(x>=0&&x<field.size()&&y>=0&&y<field.front().size()&&field[x][y]==1){
        dfs(make_pair(x,y));
      }
    }
    // postorder
  }
  // void bfs(ver_t frm){
  //   seen[frm]=0;
  //   deque<ver_t> que(1,frm); // dfs,bfs
  //   while(!que.empty()){
  //     auto u=que.front(); que.pop_front(); // bfs
  //     for(const auto& v:edge[u]){
  //       if(seen[v]==NO){
  //         seen[v]=seen[u]+1;
  //         que.push_back(v); // dfs,bfs
  //       }
  //     }
  //   }
  // }
};
int32_t main(){
  Graph g;
  // size_t n; cin>>n;
  // for(size_t i=0; i<n; ++i){
  size_t w,h; cin>>w>>h;
  while(w!=0||h!=0){
    g.field.resize(h);
    for(auto&& x:g.field){
      x.resize(w);
      for(auto&& y:x) cin>>y;
    }
    size_t ret=0;
    for(size_t i=0; i<h; ++i){
      for(size_t j=0; j<w; ++j){
        if(g.field[i][j]==1){ ++ret; g.dfs(make_pair(i,j)); }
      }
    }
    cout<<ret<<'\n';
    cin>>w>>h;
  }
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include <map>
#include <functional>
#include <cmath>
#include <cstdio>
using namespace std;

#define PI 4*atan(1)

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

void dfs(int i, int j){
  c[i][j] = 0;
  for(int dx = -1; dx <= 1; dx++){
    for(int dy = -1; dy <= 1; dy++){
      int nx = i + dx, ny = j + dy;
      if(0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1){
        dfs(nx, ny);
      }
    }
  }
}

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

int main(){
  while(cin >> w >> h && w, h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> c[i][j];

      }
    }
    solve();
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h;
vector<vector<int>> a(55, vector<int>(55, 0));
int ct;

void dfs(int y, int x) {
  a[y][x] = 0;
  for(int i = -1; i <= 1; ++i) {
    for(int j = -1; j <= 1; ++j) {
      int ny = y + i, nx = x + j;
      if(0 <= ny && ny < h && 0 <= nx && nx < w && a[ny][nx] == 1) {
        dfs(ny, nx);
      }
    }
  }
}

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", &a[i][j]);
      }
    }

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        if(a[i][j] == 1) {
          ++ct;
          dfs(i, j);
        }
      }
    }
    printf("%d\n", ct);
    ct = 0;
    a.clear();
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

typedef pair<int, int> p;

int t_x[] = {1, 1, 1, 0, -1, -1, -1, 0};
int t_y[] = {1, 0, -1, -1, -1, 0, 1, 1};

int field[52][52];  

int main()
{
  while (true) {
    memset(field, 0, sizeof field);
    int width, height;
    scanf("%d %d", &width, &height);
    
    if (width == 0 && height == 0) break;
    
    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	scanf(" %d", &field[i][j]);
      }
    }
    
    queue<p> que;
    int c = 0;

    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	if (field[i][j] == 1) {
	  c++;
	  que.push(p(i, j));
	  field[i][j] = 0;
	  while (!que.empty()) {
	    p tmp = que.front();
	    que.pop();
	    //field[tmp.second][tmp.first] = 0;
	    //cout << tmp.first << ", " << tmp.second << endl;
	    for (int k = 0; k < 8; k++) {
	      int tx = tmp.second + t_x[k];
	      int ty = tmp.first + t_y[k];
	      //cout << ty << ", "<< tx << " =  "<< field[ty][tx] << endl;
	      if (field[ty][tx] == 1) {
		field[ty][tx] = 0;
		que.push(p(ty, tx));
	      }
	    }
	    //cout << endl;
	    //cout << i << " " << j << endl;
	    for (int k = 1; k <= height; k++) {
	      for (int l = 1; l <= width; l++) {
		//cout << field[k][l] << " ";
	      }
	      //cout << endl;
	    }
	    //cout << endl;
	  }
	}
      }     
    }
    cout << c << endl;
  }
  return 0;
}

C++: #include <cstdio>

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

void dfs(int a, int b){
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			int A = a+i-1, B = b+j-1;
			if(0<=A&&A<w&&0<=B&&B<h&&c[A][B]){
				c[A][B] = 0;
				dfs(A,B);
			}
		}
	}
}

int main(){
	while(scanf("%d%d", &w, &h),w||h){
		int ans = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &c[j][i]);
			}
		}
		for(int i = 0; i < w; i++){
			for(int j = 0; j < h; j++){
				if(c[i][j]){
					c[i][j] = 0;
					ans++;
					dfs(i,j);
				}
			}
		}
		printf("%d\n", ans);
	}
}