#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int a[MAXN], b[MAXN];
int ans_1 = 0, ans_u = 0;
int _find(int x) {
  if (x == a[x]) return x;
  return a[x] = _find(a[x]);
}
void unite(int x, int y) {
  x = _find(x);
  y = _find(y);
  if (x == y) return;
  a[x] = y;
  ans_u++;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  memset(a, -1, sizeof(a));
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int len = m / 4;
    for (int j = 0; j < len; j++) {
      int x;
      if (s[j] >= 'A')
        x = s[j] - 'A' + 10;
      else
        x = s[j] - '0';
      for (int k = 0; k < 4; k++) {
        b[j * 4 + 3 - k] = x & 1;
        x >>= 1;
      }
    }
    for (int k = 0; k < m; k++) b[k] ? a[k + m] = k + m : -1;
    for (int k = 0; k < m; k++) {
      if (b[k] == 1)
        ans_1++;
      else
        continue;
      if (~a[k]) unite(k, k + m);
      if (k != 0 && b[k - 1]) unite(k + m - 1, k + m);
    }
    for (int k = 0; k < m; k++) a[k] = b[k] ? _find(k + m) - m : -1;
  }
  cout << ans_1 - ans_u << endl;
  return 0;
}
