Language: C++

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;
using R=long double;
const R EPS=1e-9; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max<R>(x,0.0));}

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

// Problem Specific Parameter:

using G=vector<vector<int>>;

auto Dreyfus_wagner(const G &graph,const auto &term){
	using W=int; W inf=1<<28;
	const int n=graph.size(),t=term.size();
	if(t<=1) return 0;
	auto dist=graph;
	vector<vector<W>> opt(1<<t,vector<W>(n,inf));

	rep(k,n)rep(i,n)rep(j,n) chmin(dist[i][j],dist[i][k]+dist[k][j]);
	
	map<int,int> dict;
	rep(i,t){
		dict[term[i]]=1<<i;
		rep(j,n) opt[1<<i][j]=dist[term[i]][j];
		rep(j,t) opt[(1<<i)|(1<<j)][term[j]]=dist[term[i]][term[j]];
	}

	auto 

	rep(k,2,t){
		for(int s=(1<<k)-1;s<(1<<t);){
			rep(i,n){
				int ns=s|dict[i];
				if(dict[i] and s==ns) continue;
				for(int u=(s-1)&s;u!=0;u=(u-1)&s){
					int c=s&(~u);
					chmin(opt[ns][i],opt[u][i]+opt[c][i]);
				}
			}
			int x=s&-s,y=s+x;
			s=((s&~y)/x>>1)|y
		}
		for(int s=(1<<k)-1;s<(1<<t);){
			rep(i,n){
				int ns=s|dict[i];
				if(dict[i] and s==ns) continue;
				rep(j,n) chmin(opt[ns][i],opt[s][j]+dist[j][i]);
			}
			int x=s&-s,y=s+x;
			s=((s&~y)/x>>1)|y;
		}
	}

	const int mask=(1<<t)-1;
	W ans=opt[mask][0];
	rep(i,n) chmin(ans,opt[mask][i]);
	return ans;
}

int main(void){
	int h,w;
	while(cin >> h >> w){
		if(h==0) break;
		const int inf=1<<28;

		G graph(h*w,vector<int>(h*w,inf));
		rep(i,h*w) graph[i][i]=0;
		vector<int> term;

		rep(i,h*w){
			int in;
			cin >> in;
			if(in) term.push_back(i);
		}
		
		rep(i,h)rep(j,w){
			const int cur=w*i+j;
			if(i+1<h) graph[cur][cur+w]=graph[cur+w][cur]=1;
			if(j+1<w) graph[cur][cur+1]=graph[cur+1][cur]=1;
		}
		int res=Dreyfus_wagner(graph,term);
		cout << h*w-1-res << endl;
	}
	return 0;
}

Compiler output:
a.cc:37:42: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   37 | auto Dreyfus_wagner(const G &graph,const auto &term){
      |                                          ^~~~
a.cc: In function 'auto Dreyfus_wagner(const G&, const auto:28&)':
a.cc:5:23: error: expected unqualified-id before 'for'
    5 | #define _range(i,a,b) for(int i=int(a);i<int(b);++i)
      |                       ^~~
a.cc:3:38: note: in expansion of macro '_range'
    3 | #define _overload(_1,_2,_3,name,...) name
      |                                      ^~~~
a.cc:6:18: note: in expansion of macro '_overload'
    6 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                  ^~~~~~~~~
a.cc:55:9: note: in expansion of macro 'rep'
   55 |         rep(k,2,t){
      |         ^~~
a.cc:55:13: error: 'k' was not declared in this scope
   55 |         rep(k,2,t){
      |             ^
a.cc:5:40: note: in definition of macro '_range'
    5 | #define _range(i,a,b) for(int i=int(a);i<int(b);++i)
      |                                        ^
a.cc:55:9: note: in expansion of macro 'rep'
   55 |         rep(k,2,t){
      |         ^~~