#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using db = long double;
using row = vector<int>;
using ii = pair<ll, ll>;
const int N = 1e6 + 5, M = 1e6 + 5, LG = 17, MOD = 1e9 + 7;
const int BLOCK = 55;
const int BLOCKN = N / BLOCK + 1;
const long double EPS = 1e-7;
using ull = unsigned long long;
int par[1 << 15];
bool g[2][1 << 14];
int get(int i) { return i == par[i] ? i : par[i] = get(par[i]); }
bitset<1 << 15> bt;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  string s;
  int ans = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> s;
    for (int j = m; j < m * 2; j++) bt.set(j);
    vector<int> v;
    for (int j = 0; j < m; j++) {
      if (g[i & 1 ^ 1][j])
        par[j] = get(par[j]);
      else
        par[j] = 0;
      bt.reset(par[j]);
    }
    for (int j = m; j < m * 2; j++)
      if (bt[j]) v.push_back(j);
    for (int j = 0; j < m >> 2; j++) {
      int val = (s[j] <= '9' ? s[j] - '0' : s[j] - 'A' + 10);
      g[i & 1][j << 2 | 3] = (val & 1);
      g[i & 1][j << 2 | 2] = (val & 2);
      g[i & 1][j << 2 | 1] = (val & 4);
      g[i & 1][j << 2] = (val & 8);
    }
    for (int j = 0, k = 0; j < m; j = k) {
      if (!g[i & 1][j])
        k = j + 1;
      else {
        vector<int> st2;
        for (k = j; k < m && g[i & 1][k]; k++) {
          if (g[i & 1 ^ 1][k]) {
            st2.push_back(get(par[k]));
          }
        }
        if (st2.empty()) {
          ans++;
          par[j] = v.back();
          par[par[j]] = par[j];
          bt[v.back()] = 0;
          v.pop_back();
          for (int x = j + 1; x < k; x++) par[x] = par[x - 1];
        } else {
          for (auto x : st2) {
            if (get(x) != get(st2[0])) {
              ans--;
              par[x] = get(st2[0]);
            }
          }
          par[j] = get(st2[0]);
          for (int x = j + 1; x < k; x++) par[x] = par[x - 1];
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
