#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx,tune=native")
using namespace std;
const int M = (1 << 14) + 8;
class DJS {
 private:
  vector<int_fast32_t> arr;

 public:
  inline void init(int_fast32_t n) {
    arr.resize(n);
    iota(begin(arr), end(arr), 0);
  }
  inline void rebuild() { iota(begin(arr), end(arr), 0); }
  inline int_fast32_t query(int_fast32_t x) {
    if (arr[x] != x) arr[x] = query(arr[x]);
    return arr[x];
  }
  inline void merge(int_fast32_t a, int_fast32_t b) {
    arr[query(a)] = arr[query(b)];
  }
} djs;
bool hve[M << 1];
int_fast32_t id[2][M];
bitset<M> me, he;
inline void init(int_fast32_t);
inline void parse(const string&, bitset<M>&);
inline int_fast32_t hex2dec(char);
inline void build(int_fast32_t[], int_fast32_t[], int_fast32_t);
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int_fast32_t n, m, ans = 0;
  cin >> n >> m;
  init(m);
  int_fast32_t a = 0, b = 1;
  for (int_fast32_t i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    parse(ss, me);
    build(id[a], id[b], m);
    djs.rebuild();
    bool pre = false;
    for (int_fast32_t j = 0; j < m; j++) {
      if (me[j]) {
        ans++;
        if (pre) {
          djs.merge(id[a][j], id[a][j - 1]);
          ans--;
        } else
          pre = true;
        if (he[j] and djs.query(id[b][j]) != djs.query(id[a][j])) {
          djs.merge(id[b][j], id[a][j]);
          ans--;
        }
      } else
        pre = false;
    }
    swap(me, he);
    swap(a, b);
  }
  cout << ans << '\n';
  return 0;
}
inline void init(int_fast32_t n) {
  for (int_fast32_t i = 0; i < n; i++) id[0][i] = i;
  for (int_fast32_t i = 0; i < n; i++) id[1][i] = i + n;
  djs.init(2 * n);
}
inline int_fast32_t hex2dec(char c) {
  if (isdigit(c)) return c - '0';
  return c - 'A' + 10;
}
inline void parse(const string& ss, bitset<M>& ret) {
  int_fast32_t n = ss.size(), cur = 0;
  for (int_fast32_t i = 0; i < n; i++) {
    int_fast32_t a = hex2dec(ss[i]);
    for (int_fast32_t j = 3; j >= 0; j--, cur++) ret[cur] = (a >> j) & 1;
  }
}
inline void build(int_fast32_t a1[], int_fast32_t a2[], int_fast32_t n) {
  memset(hve, 0, sizeof(hve));
  for (int_fast32_t i = 0; i < n; i++) {
    a2[i] = djs.query(a2[i]);
    hve[a2[i]] = 1;
  }
  int_fast32_t cur = 0;
  for (int_fast32_t i = 0; i < n; i++) {
    while (hve[cur]) cur++;
    a1[i] = cur++;
  }
}
