"use strict";

function main() {
  var i = 0;

  while (true) {
    var H = parseInt(input[i].split(' ')[1], 10);
    culc(input.slice(i, i + H + 1));
    i += H + 1;

    if (input[i] === '0 0') {
      break;
    }
  }
}

function culc(_input) {
  var args = _input;
  var W = args[0].split(' ')[0];
  var H = args[0].split(' ')[1];
  var graph = new Array(H);
  var seen = new Array(H);

  for (var i = 0; i < H; i++) {
    graph[i] = new Array(W);
    seen[i] = new Array(W);
    var line = args[i + 1].split(' ');

    for (var j = 0; j < W; j++) {
      graph[i][j] = line[j];
      seen[i][j] = false;
    }
  }

  var count = 0;

  for (var _i = 0; _i < H; _i++) {
    for (var _j = 0; _j < W; _j++) {
      if (seen[_i][_j]) continue;
      if (graph[_i][_j] == 0) continue;
      count++;
      dfs(graph, seen, _j, _i);
    }
  }

  console.log(count);
}

var dx = [1, 1, 0, -1, -1, -1, 0, 1];
var dy = [0, 1, 1, 1, 0, -1, -1, -1];

function dfs(graph, seen, startx, starty) {
  seen[starty][startx] = true;

  for (var i = 0; i < 8; i++) {
    var nextx = startx + dx[i];
    var nexty = starty + dy[i];
    if (nextx >= graph[0].length || nextx < 0) continue;
    if (nexty >= graph.length || nexty < 0) continue;
    if (graph[nexty][nextx] == 0) continue;
    if (seen[nexty][nextx]) continue;
    dfs(graph, seen, nextx, nexty);
  }
}

var input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
  input += chunk;
});
process.stdin.on('end', function () {
  input = input.split('\n');
  main();
});
