#include <bits/stdc++.h>
using namespace std;
struct dsu {
  vector<int> link, size;
  dsu(int n) {
    link.resize(n + 1);
    size.assign(n + 1, 1);
    for (int i = 0; i <= n; i++) {
      link[i] = i;
    }
  }
  int root(int a) {
    while (a != link[a]) {
      link[a] = link[link[a]];
      a = link[a];
    }
    return a;
  }
  bool find(int a, int b) { return (root(a) == root(b)); }
  void join(int a, int b) {
    int x1 = root(a), x2 = root(b);
    if (x1 == x2) return;
    auto &a1 = size[x1], &a2 = size[x2];
    if (a1 > a2) {
      a1 += a2;
      link[x2] = x1;
    } else {
      a2 += a1;
      link[x1] = x2;
    }
  }
  void clear() {
    for (int i = 0; i < link.size(); i++) {
      link[i] = i;
    }
    size.assign(size.size(), 1);
  }
};
inline unsigned int hex2bin(char &hex) {
  if (hex >= '0' && hex <= '9') return hex - '0';
  return 10 + hex - 'A';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, cnt, ans = 0;
  char ch;
  cin >> n >> m;
  vector<int> indexes(m, 0), nindex(m, 0), vis1(m + 1, 0);
  for (int i = 0; i < n; i++) {
    cnt = 1;
    dsu d(m + 1);
    for (int j = 0; j < (m / 4); j++) {
      cin >> ch;
      ch = hex2bin(ch);
      for (int k = 0; k < 4; k++) {
        int l = 4 * j + k;
        if (ch & (1 << (3 - k))) {
          if (indexes[l]) {
            vis1[indexes[l]] = 2;
            nindex[l] = indexes[l];
            if (l && nindex[l - 1] && (nindex[l] != nindex[l - 1])) {
              d.join(nindex[l - 1], indexes[l]);
            }
          } else if (l && nindex[l - 1]) {
            nindex[l] = nindex[l - 1];
          } else {
            while (vis1[cnt]) cnt++;
            nindex[l] = cnt++;
          }
        } else {
          nindex[l] = 0;
        }
      }
    }
    for (int j = 0; j < m; j++) {
      if (nindex[j]) {
        indexes[j] = d.root(nindex[j]);
      } else {
        indexes[j] = 0;
      }
    }
    for (int j = 1; j <= m; j++) {
      if (vis1[j] == 1) ans++;
      vis1[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      if (indexes[j]) vis1[indexes[j]] = 1;
    }
  }
  for (int i = 1; i <= m; i++)
    if (vis1[i] == 1) {
      ans++;
    }
  cout << ans;
  return 0;
}
