#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for (auto &aa : a) {
        for (auto &aaa : aa) {
            cin >> aaa;
        }
    }
    int ans = 0, dd[] = {0, 1, 0, -1, 0};
    function<void(int, int)> f = [&](int x, int y) {
        if (a[x][y] != 1) {
            return;
        }
        a[x][y] = 2;
        for (int i = 0; i < 4; i++) {
            int nx = x + dd[i], ny = y + dd[i + 1];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
                f(nx, ny);
            }
        }
    };
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == 1) {
                f(i, j);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}