Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

int field[50][50];
int w, h;
vector<int> di_x = {1,0,-1,0,1,-1,1,-1};
vector<int> di_y = {0,1,0,-1,1,1,-1,-1};

void dfs(int x, int y) {
   field[x][y] = 0;
   rep(i,8) {
      int n_x = x + di_x[i];
      int n_y = y + di_y[i];
      if (n_x < 0 || h <= n_x || n_y < 0 || w <= n_y) continue;
      else if (field[n_x][n_y] == 0) continue;
      else dfs(n_x,n_y);
   }
}

int main () {
   while (true) {
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      int ct = 0;
      rep(i,h)rep(j,w) cin >> field[i][j];
      rep(i,h)rep(j,w) {
         if (field[i][j] == 1) {
            dfs(i,j);
            ct++;
         }
      }
      cout << ct << endl;
      rep(i,h)rep(j,w) field[i][j] = 0;
   }
}

C++: #include <iostream>
using namespace std;
int totalRow, totalColumn;


void dfs(int input[50][50], bool visitedIsland[50][50],int row, int col)
{
    //cout << row << " " << col << endl ;
    visitedIsland[row][col] = true;
    if (row < totalRow   && col < totalColumn )
    {
        //Up
        if (row > 0 && input[row - 1][col] == 1 && visitedIsland[row - 1][col] == false)
            dfs(input,visitedIsland,row - 1,col);
        //cout << "up " << row << " " << col << input[row - 1][col] << " " << visitedIsland[row - 1][col] << endl;
        //Down
        if ((row + 1 <= totalRow - 1) && input[row + 1][col] == 1 && visitedIsland[row + 1][col]== false)
            dfs(input,visitedIsland,row + 1,col);
        //cout << "down " << input[1][col] << " " <<visitedIsland[row + 1][col] << endl;
        //Left
        if (col > 0 && input[row][col-1] == 1 && visitedIsland[row][col-1]== false)
            dfs(input,visitedIsland,row,col - 1);
        //cout << "left" << endl;
        //Right
        if ((col + 1 <= totalColumn - 1)&&input[row][col + 1] == 1 && visitedIsland[row][col+1]== false)
            dfs(input,visitedIsland,row,col + 1);
        //Left bottom diagonal
        if (row > 0 && col > 0 && input[row - 1][col - 1] == 1 && visitedIsland[row - 1][col - 1]== false)
            dfs(input,visitedIsland,row - 1,col - 1);
        //cout << "Left bottom " << row << " " << col << input[row - 1][col-1] << " " << visitedIsland[row - 1][col-1] << endl;
        //Left top diagonal
        if ((row + 1 <= totalRow - 1) && col > 0 && input[row + 1][col - 1] == 1 && visitedIsland[row + 1][col - 1]== false)
            dfs(input,visitedIsland,row + 1,col - 1);
        //Right top diagonal
        if ((col + 1 <= totalColumn - 1)&&row > 0 && input[row - 1][col + 1] == 1 && visitedIsland[row - 1][col + 1]== false)
            dfs(input,visitedIsland,row - 1,col +1);
        //cout << "Right top " << row << " " << col << input[row - 1][col+1] << " " << visitedIsland[row - 1][col+1] << endl;
        //Right bottom diagonal
        if ((row + 1 <= totalRow - 1)&&(col + 1 <= totalColumn - 1)&& input[row + 1][col + 1] == 1 && visitedIsland[row + 1][col + 1]== false)
            dfs(input,visitedIsland,row + 1,col +1);
    }
    
}

int main(int argc, const char * argv[]) {
    int row, col;
    while (cin >> col >> row)
    {
        totalRow = row;
        totalColumn = col;
        int numofIsland = 0;
        int input[50][50] = {0};
        bool visitedIsland[50][50] = {false};
        for (int i= 0; i < row; i++)
            for (int j = 0; j < col; j++)
                cin >> input[i][j];
        //checking
        //cout << input[1][0] << " " << input[0][0]<< endl;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (input[i][j] == 1 && !visitedIsland[i][j])
                {
                    //push neighbour
                    numofIsland++;
                    //processed = true;
                    //cout << i << " " << j << endl ;
                    dfs(input,visitedIsland,i,j);
                }
            }
        }
        if (row > 0 &&  col > 0)
            cout << numofIsland << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

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

void dfs(int x, int y)
{
    field[y][x] = -1;

    for (int i = 0; i < 8; i++)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;

        if (field[ny][nx] == 1)
            dfs(nx, ny);
    }
}

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

        int ans = 0;

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

        cout << ans << endl;
    }

    return 0;
}