#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;
using vll = vector<ll>;
using vld = vector<ld>;
#define _GLIBCXX_DEBUG
#define rep(j, m) for (ll j = 0; j < (ll)(m); j++)
#define rep2(i, l, n) for (ll i = l; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
const ld PI = 3.1415926535897932;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;


int main() {
    ll H,W;
    cin >> H >> W;
    vector<string> V(H);
    rep(i,H) {cin >> V[i];}
    vll B(H,0);
    vll b(W,0);
    ll ans = 0;
    rep(i,H) {
        if (B[i] == 0) {
            ll flag = 0;
            queue<ll> Q;
            Q.push(i);
            B[i]++;
            while(Q.size()) {
                rep(j,W) {
                    if (V[Q.front()][j] == '#' && b[j] == 0) {
                        b[j]++;
                        if (j == 0 || j == W - 1) {flag++;}
                        rep(k,H) {
                            if (V[k][j] == '#') {
                                if (B[k] == 0) {
                                    Q.push(k);
                                    B[k]++;
                                }
                            }
                        }
                    }
                }
                if (Q.front() == 0 || Q.front() == H - 1) {flag++;}
                Q.pop();
            }
            if (flag == 0) {
                ans++;
            }
        }
    }
    vll C(W,0);
    vll c(H,0);
    ll ans2 = 0;
    rep(i,W) {
        if (C[i] == 0) {
            ll flag = 0;
            queue<ll> Q;
            Q.push(i);
            C[i]++;
            while(Q.size()) {
                rep(j,H) {
                    if (V[j][Q.front()] == '#' && c[j] == 0) {
                        c[j]++;
                        if (j == 0 || j == H - 1) {flag++;}
                        rep(k,W) {
                            if (V[j][k] == '#') {
                                if (C[k] == 0) {
                                    Q.push(k);
                                    C[k]++;
                                }
                            }
                        }
                    }
                }
                if (Q.front() == 0 || Q.front() == W - 1) {flag++;}
                Q.pop();
            }
            if (flag == 0) {
                ans2++;
            }
        }
    }
    cout << min(ans,ans2) << endl;
}

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>
#include <cmath>
#include <tuple>
#if defined _DEBUG
#include "TestCase.h"
#include "Util.h"
#endif

using namespace std;

typedef int64_t ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef tuple<int, int, int> tiii;

#define PI 3.14159265358979

static const int Mod = 1000000007;

bool Next(vector<int> &border, int maxNum)
{
	int size = border.size();
	int i = 1;
	bool ret = true;
	if (border[size - i] < maxNum - i + 1)
	{
		border[size - i]++;
	}
	else if (size == i)
	{
		ret = false;
	}
	else
	{
		bool flag = true;
		while (flag)
		{
			i++;
			if (border[size - i] < maxNum - i + 1)
			{
				border[size - i]++;
				for (int j = i - 1; j >= 1; j--)
				{
					border[size - j] = border[size - j - 1] + 1;
				}
				flag = false;
			}
			else if (i == size)
			{
				flag = false;
				ret = false;
			}
		}
	}

	return ret;
}

int main(void)
{
#if 0
	vector<pair<ll, ll>> format(2);
	//format[0] = make_pair(1, 1000000000);
	format[0] = make_pair(1, 100);
	format[1] = make_pair(1, 100);
	TestCase::MakeRandom(30, format);
	return 0;
#endif
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++)
		cin >> A[i];

	// 分割ナシ
	int minval = A[0];
	for (int i = 1; i < N; i++)
	{
		minval |= A[i];
	}

	// 分割アリ
	for (int S = 2; S <= N; S++)	// 分割数
	{
		vector<int> border(S-1);	// 境界
		// 境界の初期化
		for (int i = 1; i < S; i++)
		{
			border[i - 1] = i;
		}
		//do
		//{
		//	for (int b : border)
		//		cout << b << " ";
		//	cout << endl;
		//} while (Next(border, N));
		do
		{
			//cout << "border : ";
			//	for (int b : border)
			//		cout << b << " ";
			//	cout << endl;
			vector<int> vals(S);
			int begin = 0;
			for (int s = 0; s < S && begin < N; s++)
			{
				int end;
				if (s < border.size())
					end = border[s];
				else
					end = N;
				vals[s] = A[begin];
				for (int i = begin; i < end; i++)
				{
					vals[s] |= A[i];
				}
				begin = end;
				//cout << "vals[" << s << "] = " <<  vals[s] << " ";
			}
			//cout << endl;
			int val = vals[0];
			for (int s = 1; s < S; s++)
			{
				val = val ^ vals[s];
			}
			minval = min(minval, val);
		}
		while (Next(border, N));
	}

	cout << minval << endl;

	return 0;
}

