#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, int>;

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

int H, W;

bool f[3000][3000];

void dfs(int ch, int cw) {
    stack<P> s;
    s.emplace(ch, cw);
    f[ch][cw] = false;
    while (!s.empty()) {
        P p = s.top();
        s.pop();
        for (int i = 0; i < 4; i++) {
            int nh = p.first + dh[i], nw = p.second + dw[i];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (!f[nh][nw]) continue;
            f[nh][nw] = false;
            s.emplace(nh, nw);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> f[i][j];
        }
    }

    int ans = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (f[i][j]) {
                dfs(i, j);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}