#include <bits/stdc++.h>
using namespace std;
void addmod(int &a, long long b) {
  a = (a + b);
  if (a >= 1000000007) a -= 1000000007;
}
void mulmod(int &a, long long b) { a = (a * b) % 1000000007; }
template <class T>
bool domin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool domax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
int gi() {
  int a;
  scanf("%d", &a);
  return a;
}
long long gll() {
  long long a;
  scanf("%lld", &a);
  return a;
}
int bits[2][16388];
class disjoint {
 public:
  vector<int> p;
  vector<bool> seen;
  disjoint() : p(2 * 16388, -1), seen(2 * 16388, false) {}
  void clear(int ix) {
    if (ix)
      for (int i = 16388; i < 2 * 16388; i++) p[i] = -1;
    else
      for (int i = 0; i < 16388; i++) p[i] = -1;
  }
  int find(int ix) { return p[ix] < 0 ? ix : p[ix] = find(p[ix]); }
  bool join(int ixa, int a, int ixb, int b) {
    if (ixa) a += 16388;
    if (ixb) b += 16388;
    if ((a = find(a)) == (b = find(b))) return false;
    p[a] += p[b];
    p[b] = a;
    return true;
  }
  int cnt(int ix, int m) {
    int ans = 0;
    if (ix) {
      for (int i = 16388; i < 16388 + m; i++) seen[i] = false;
      for (int i = 16388; i < 16388 + m; i++)
        if (bits[ix][i - 16388]) {
          int v = find(i);
          if (v >= 16388 && !seen[v]) ans++, seen[v] = true;
        }
    } else {
      for (int i = 0; i < 16388; i++) seen[i] = false;
      for (int i = 0; i < m; i++)
        if (bits[ix][i]) {
          int v = find(i);
          if (v < 16388 && !seen[v]) ans++, seen[v] = true;
          ;
        }
    }
    return ans;
  }
};
int main() {
  int n = gi(), m = gi();
  char *s;
  s = new char[16388];
  int ix = 1, px = 0, ans = 0;
  disjoint dj;
  for (int in = 0; in < n; in++) {
    ix = px;
    px = ix ^ 1;
    dj.clear(ix);
    scanf("%s", s);
    for (int j = 0; j < m; j += 4) {
      int ti = j / 4;
      int v =
          (s[ti] >= 'A' && s[ti] <= 'F') ? (10 + (s[ti] - 'A')) : s[ti] - '0';
      for (int k = 3; k >= 0; k--) {
        bits[ix][j + k] = (v & 1);
        v /= 2;
      }
    }
    for (int j = 0; j < m; j++)
      if (bits[ix][j]) {
        if (j && bits[ix][j - 1]) dj.join(ix, j - 1, ix, j);
        if (bits[px][j]) dj.join(ix, j, px, j);
      }
    ans += dj.cnt(px, m);
  }
  ans += dj.cnt(ix, m);
  cout << ans << endl;
  return 0;
}
