1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
/*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\pali\src\main\pali.hpp]]}) ===*/
/*
  {   -- filePaths
    [[C:\Projekty\r e p o\pali\src\main\pali.hpp]],
  },
*/
#ifndef PALI_HPP
#define PALI_HPP

///
///
///
/*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]]}) ===*/
/*
  {   -- filePaths
    [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]],
  },
*/
// Based on no_sillywarnings_please, Copyright (c) Alf P. Steinbach, 2010.
// See https://alfps.wordpress.com/the-no_sillywarnings_please-h-file/ .
// Extra warnings added for /Wall.

#ifndef PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H
#define PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H

#ifdef _MSC_VER

// The following are real warnings but are generated by almost all MS headers, including
// standard library headers, so it's impractical to leave them on.
#pragma  warning( disable: 4619 )   // there is no warning number 'XXXX'
#pragma  warning( disable: 4668 )   // XXX is not defined as a preprocessor macro

// The following are pure sillywarnings:
#pragma warning( disable: 4061 )    // enum value is not *explicitly* handled in switch
#pragma warning( disable: 4099 )    // first seen using 'struct' now seen using 'class'
#pragma warning( disable: 4127 )    // conditional expression is constant
#pragma warning( disable: 4217 )    // member template isn't copy constructor
#pragma warning( disable: 4250 )    // inherits (implements) some member via dominance
#pragma warning( disable: 4251 )    // needs to have dll-interface to be used by clients
#pragma warning( disable: 4275 )    // exported class derived from non-exported class
#pragma warning( disable: 4347 )    // "behavior change", function called instead of template
#pragma warning( disable: 4355 )    // "'this': used in member initializer list
#pragma warning( disable: 4428 )    // MSVC 9: universal-character-name encountered in source
#pragma warning( disable: 4505 )    // unreferenced function has been removed
#pragma warning( disable: 4510 )    // default constructor could not be generated
#pragma warning( disable: 4511 )    // copy constructor could not be generated
#pragma warning( disable: 4512 )    // assignment operator could not be generated
#pragma warning( disable: 4513 )    // destructor could not be generated
#pragma warning( disable: 4610 )    // can never be instantiated user defined constructor required
#pragma warning( disable: 4623 )    // default constructor could not be generated
#pragma warning( disable: 4624 )    // destructor could not be generated
#pragma warning( disable: 4625 )    // copy constructor could not be generated
#pragma warning( disable: 4626 )    // assignment operator could not be generated
#pragma warning( disable: 4640 )    // a local static object is not thread-safe
#pragma warning( disable: 4661 )    // a member of the template class is not defined.
#pragma warning( disable: 4670 )    // a base class of an exception class is inaccessible for catch
#pragma warning( disable: 4672 )    // a base class of an exception class is ambiguous for catch
#pragma warning( disable: 4673 )    // a base class of an exception class is inaccessible for catch
#pragma warning( disable: 4675 )    // resolved overload was found by argument-dependent lookup
#pragma warning( disable: 4702 )    // unreachable code, e.g. in <list> header.
#pragma warning( disable: 4710 )    // call was not inlined
#pragma warning( disable: 4711 )    // call was inlined
#pragma warning( disable: 4820 )    // some padding was added
#pragma warning( disable: 4917 )    // a GUID can only be associated with a class, interface or namespace
#pragma warning( disable: 4996 )    // MSVC 9: a C std library function has been "deprecated" (says MS)

// Extra for /Wall:
#pragma warning( disable: 4350 )    // behavior change: 'member1' called instead of 'member2'
#pragma warning( disable: 4351 )    // new behavior: elements of array 'array' will be default initialized
#pragma warning( disable: 4514 )    // 'function' : unreferenced inline function has been removed
#pragma warning( disable: 4718 )    // 'function call' : recursive call has no side effects, deleting

#endif   // _MSC_VER

#endif   // PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H

/*}}}*/



///
///
///
/*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]]}) ===*/
/*
  {   -- filePaths
    [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]],
  },
*/
#ifdef _MSC_VER
#   pragma warning( push )
#   pragma warning( disable: 4189 )     // 'identifier' : local variable is initialized but not referenced
#   pragma warning( disable: 4191 )
#   pragma warning( disable: 4242 )
#   pragma warning( disable: 4244 )
#   pragma warning( disable: 4267 )
#   pragma warning( disable: 4350 )
#   pragma warning( disable: 4365 )
#   pragma warning( disable: 4371 )
#   pragma warning( disable: 4548 )
#   pragma warning( disable: 4738 )
#   pragma warning( disable: 4774 )     // '<function>' : format string expected in argument <position> is not a string literal
#   pragma warning( disable: 4820 )
#   pragma warning( disable: 4987 )
#   pragma warning( disable: 5026 )
#   pragma warning( disable: 5027 )
#endif

#ifdef __GNUC__
#   if __GNUC__ >= 4
#       if __GNUC_MINOR__ >= 6
#           pragma GCC diagnostic push
#           pragma GCC diagnostic ignored "-Weffc++"
#           pragma GCC diagnostic ignored "-Wconversion"
#           pragma GCC diagnostic ignored "-Wsign-conversion"
#           pragma GCC diagnostic ignored "-Wshadow"
#           pragma GCC diagnostic ignored "-Wstrict-aliasing"
#       endif
#   endif
#endif

/*}}}*/

#include <cstddef>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <memory>
#include <limits>
#include <iostream>

/*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]]}) ===*/
/*
  {   -- filePaths
    [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]],
  },
*/
#ifdef _MSC_VER
#   pragma warning( disable: 5031 )     // #pragma warning(pop): likely mismatch, popping warning state pushed in different file
#   pragma warning( pop )
#endif

#ifdef __GNUC__
#   if __GNUC__ >= 4
#       if __GNUC_MINOR__ >= 6
#           pragma GCC diagnostic pop
#       endif
#   endif
#endif

/*}}}*/



///
///
///
void noop(...) { }



///
///
///
template<typename T>
void debug_echo(T x, const char* name, const char* delim, int line)
{
    std::cout
        << "DEBUG_ECHO   " << name << ": "
        << delim << x << delim
        << "   (line " << line << ")"
        << '\n';
}

///
///
///
#define DEBUG_ECHO(x) do { debug_echo(x, #x, "", __LINE__); } while (0)

///
///
///
#define DEBUG_ECHO_DELIM(x, delim) do { debug_echo(x, #x, delim, __LINE__); } while (0)



namespace pali {

///
///
///
typedef size_t umsize_t;
typedef ptrdiff_t msize_t;
typedef size_t umindex_t;
typedef ptrdiff_t mindex_t;
typedef ptrdiff_t munidiff_t;

const mindex_t Null_Index = -1;

const int Uninitialized_Positively = 7777777;
const int Uninitialized_Negatively = -Uninitialized_Positively;

}



///
///
///
#ifdef OVERRIDE_NEW_DELETE

namespace pali { namespace new_alloc {
    msize_t _allocated;
    msize_t _times;

    msize_t get_allocated()
    {
        return _allocated;
    }

    msize_t get_times()
    {
        return _times;
    }

    void clear_counters()
    {
        _allocated = 0;
        _times = 0;
    }
}}

void* operator new(size_t size)
{
    pali::new_alloc::_allocated += static_cast<pali::msize_t>(size);
    ++pali::new_alloc::_times;
    return malloc(size);
}

void* operator new[](size_t size)
{
    pali::new_alloc::_allocated += static_cast<pali::msize_t>(size);
    ++pali::new_alloc::_times;
    return malloc(size);
}

void operator delete(void* ptr) noexcept
{
    free(ptr);
}

void operator delete[](void* ptr) noexcept
{
    free(ptr);
}

#endif   // OVERRIDE_NEW_DELETE



namespace pali {

namespace alloc {

///
///
///
class Pool
{
public:
    Pool(msize_t size)
        : _size(size)
        , _pool(new char[static_cast<umsize_t>(_size)])
        , _allocated(0)
        , _times(0)
    { }

    ~Pool()
    {
        delete[] _pool;
    }

    template<typename T>
    void* allocate(msize_t cnt)
    {
        if (cnt != 1) {
            noop();
        }
        auto size = static_cast<msize_t>(cnt * sizeof(T));
        if (_allocated + size > _size) {
            throw std::bad_alloc();
        }
        auto result = _pool + _allocated;
        _allocated += size;
        ++_times;
        return result;
    }

    void deallocate(void* /*ptr*/)
    {
    }

    msize_t get_allocated() const
    {
        return _allocated;
    }

    msize_t get_times() const
    {
        return _times;
    }

private:
    Pool(const Pool&);
    Pool& operator=(const Pool&);

    msize_t const _size;
    char* const _pool;
    msize_t _allocated;
    msize_t _times;
};



///
///
///
template<typename T>
class CustomAllocator
{
public:
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;

    template<typename U>
    struct rebind { typedef CustomAllocator<U> other; };

    CustomAllocator(Pool& pool)
        : _pool(pool)
    { }

    template<typename U>
    CustomAllocator(const CustomAllocator<U>& other)
        : _pool(other._pool)
    { }

    value_type* allocate(size_t cnt)
    {
        //return static_cast<value_type*>(::operator new(cnt * sizeof(value_type)));

        return static_cast<value_type*>(_pool.allocate<value_type>(static_cast<msize_t>(cnt)));
    }

    void deallocate(value_type* ptr, size_t)
    {
        //::operator delete(ptr);

        _pool.deallocate(ptr);
    }

    void construct(value_type* ptr, const value_type& t) const { new(ptr) value_type(t); }
    void destroy(value_type* ptr) const { /* MSVC C4100 workaround */noop(ptr); ptr->~value_type(); }

    Pool& _pool;
};

template<typename T, typename U>
bool operator==(CustomAllocator<T> const&, CustomAllocator<U> const&)
{
    return true;
}

template<typename T, typename U>
bool operator!=(CustomAllocator<T> const& x, CustomAllocator<U> const& y)
{
    return !(x == y);
}

}   // namespace alloc



namespace math {

///
///
///
int inverse(int x, int mod)
{
    int t = 0; int newt = 1;
    int r = mod; int newr = x;
    while (newr != 0) {
        int quotient = r / newr;
        int tmp;

        tmp = t;
        t = newt;
        newt = tmp - quotient * newt;

        tmp = r;
        r = newr;
        newr = tmp - quotient * newr;
    }
    assert(r == 1);
    if (t < 0) {
        t += mod;
    }
    return t;
}

}   // namespace math



namespace hash {

///
///
///
struct RabinKarpHasher
{
    RabinKarpHasher(msize_t size_max)
        : Base_To_Size(nullptr)
        , Inverse_Base_To_Size(nullptr)
    {
        Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)];
        Inverse_Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)];

        long long value = 1;
        Base_To_Size[0] = static_cast<int>(value);
        Inverse_Base_To_Size[0] = pali::math::inverse(static_cast<int>(value), Mod);
        for (mindex_t i=1; i <= size_max; ++i) {
            value *= Base;
            value %= Mod;
            Base_To_Size[i] = static_cast<int>(value);
            Inverse_Base_To_Size[i] = pali::math::inverse(static_cast<int>(value), Mod);
        }
    }

    ~RabinKarpHasher()
    {
        delete[] Base_To_Size;
        delete[] Inverse_Base_To_Size;
    }

    int hash(const std::vector<int>& tab, mindex_t from, msize_t size)
    {
        long long result = 0;
        for (mindex_t i=from; i <= from + size - 1; ++i) {
            result *= Base;
            result %= Mod;
            result += hv(tab[static_cast<umindex_t>(i)]);
            result %= Mod;
        }
        return static_cast<int>(result);
    }

    int hash(const int* tab, mindex_t from, msize_t size)
    {
        long long result = 0;
        for (mindex_t i=from; i <= from + size - 1; ++i) {
            result *= Base;
            result %= Mod;
            result += hv(tab[i]);
            result %= Mod;
        }
        return static_cast<int>(result);
    }

    int hash_next(const std::vector<int>& tab, int prev, mindex_t to)
    {
        long long result = prev;
        result *= Base;
        result %= Mod;
        result += hv(tab[static_cast<umindex_t>(to)]);
        result %= Mod;
        return static_cast<int>(result);
    }

    int hash_next(const int* tab, int prev, mindex_t to)
    {
        long long result = prev;
        result *= Base;
        result %= Mod;
        result += hv(tab[to]);
        result %= Mod;
        return static_cast<int>(result);
    }

    int hash_roll(const std::vector<int>& tab, int prev, mindex_t to, msize_t size)
    {
        long long result = prev;
        result *= Base;
        result %= Mod;
        result += hv(tab[static_cast<umindex_t>(to)]);
        result %= Mod;
        long long val = Base_To_Size[size] * hv(tab[static_cast<umindex_t>(to - size)]);
        val %= Mod;
        result += Mod - val;
        result %= Mod;
        return static_cast<int>(result);
    }

    int hash_roll(const int* tab, int prev, mindex_t to, msize_t size)
    {
        long long result = prev;
        result *= Base;
        result %= Mod;
        result += hv(tab[to]);
        result %= Mod;
        long long val = Base_To_Size[size] * hv(tab[to - size]);
        val %= Mod;
        result += Mod - val;
        result %= Mod;
        return static_cast<int>(result);
    }

    int sub_hash(int a, int b, int pos_diff)
    {
        long long result = a;
        result += Mod;
        result -= b;
        result *= Inverse_Base_To_Size[pos_diff];
        result %= Mod;
        return static_cast<int>(result);
    }

private:
    RabinKarpHasher(const RabinKarpHasher&);
    RabinKarpHasher& operator=(const RabinKarpHasher&);

    static const int Base = 131071;
    static const int Mod = 524287;
    int* Base_To_Size;
    int* Inverse_Base_To_Size;

    int hv(int x)
    {
        return x;
    }
};

}   // namespace hash

}   // namespace pali

#endif   // PALI_HPP

/*}}}*/



#include "dzialka.h"
#include "message.h"



#define V4



#ifdef V1
#include <disable_warnings.h>
#include <iomanip>
#include <reenable_warnings.h>
namespace v1 {
    const int Side_max = 75000;

    struct ColumnAndDepth
    {
        int column, depth;
    };

    int depths[Side_max];
    ColumnAndDepth columnAndDepths[Side_max];

    long long solve()
    {
        const int Num_Rows = GetFieldHeight();
        const int Num_Cols = GetFieldWidth();

        long long result = 0;
        int columnAndDepths_len = 0;
        for (int row = 0; row < Num_Rows; ++row) {
            for (int col = 0; col < Num_Cols; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col];
                } else {
                    depths[col] = 0;
                }
                int depth = depths[col];

                int i = columnAndDepths_len - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i].depth > depth) {
                    earliest = columnAndDepths[i].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i].depth < depth) {
                        ++i;
                        columnAndDepths[i].column = earliest;
                        columnAndDepths[i].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i].column = earliest;
                    columnAndDepths[i].depth = depth;
                }
                columnAndDepths_len = i + 1;

                int prev_depth = 0;
                for (i = 0; i < columnAndDepths_len; ++i) {
                    long long val = (col - columnAndDepths[i].column + 1) * (columnAndDepths[i].depth - prev_depth);
                    result += val;
                    prev_depth = columnAndDepths[i].depth;
                }
            }

            columnAndDepths_len = 0;
        }
        return result;
    }
}
#endif



#ifdef V2
#include <disable_warnings.h>
#include <chrono>
#include <thread>
#include <mutex>
#include <iomanip>
#include <reenable_warnings.h>
namespace v2 {
    const int Number_Of_Nodes = 5;
    const int Buf_Size = 1000000;

    int int_buf[Number_Of_Nodes][Number_Of_Nodes][Buf_Size];
    int int_buf_write_to[Number_Of_Nodes][Number_Of_Nodes];
    int int_buf_read_from[Number_Of_Nodes][Number_Of_Nodes];
    void PutInt(int me, int target, int value)
    {
        if (int_buf_write_to[me][target] == Buf_Size) {
            throw std::runtime_error("PutInt");
        }
        int_buf[me][target][int_buf_write_to[me][target]++] = value;
    }
    int GetInt(int me, int source)
    {
        while (int_buf_read_from[source][me] >= int_buf_write_to[source][me]) {
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
        return int_buf[source][me][int_buf_read_from[source][me]++];
    }

    std::mutex mtx;



    using pali::Uninitialized_Negatively;

    const int Side_max = 75000;
    const int Row_Chunk_Size = 100;

    struct ColumnAndDepth
    {
        int column, depth;
    };

    int depths_initial_for_chunk[Side_max][Number_Of_Nodes];
    int depths[Side_max][Number_Of_Nodes];
    int min_depth[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_len[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths_receive[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_receive_len[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths_send[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_send_len[Row_Chunk_Size][Number_Of_Nodes];
    long long node_result[Number_Of_Nodes];

    void solve_node_calculate(int nodeId, int from_row, int to_row)
    {
        const int Num_Rows = GetFieldHeight();
        const int Num_Cols = GetFieldWidth();

        const int Col_Chunk = Num_Cols / Number_Of_Nodes + 1;
        const int First_Col = nodeId * Col_Chunk;
        const int Bound_Col = std::min((nodeId + 1) * Col_Chunk, Num_Cols);

        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i][nodeId] = depths_initial_for_chunk[i][nodeId];
        }
        for (int row = from_row; row <= to_row; ++row) {
            for (int i = 0; i < columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId]; ++i) {
                columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].column;
                columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].depth;
            }
            columnAndDepths_len[row % Row_Chunk_Size][nodeId] = columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId];
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col][nodeId];
                } else {
                    depths[col][nodeId] = 0;
                }
                int depth = depths[col][nodeId];

                int i = columnAndDepths_len[row % Row_Chunk_Size][nodeId] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth > depth) {
                    earliest = columnAndDepths[i][row % Row_Chunk_Size][nodeId].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth < depth) {
                        ++i;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                }
                columnAndDepths_len[row % Row_Chunk_Size][nodeId] = i + 1;

                int prev_depth = 0;
                for (i = 0; i < columnAndDepths_len[row % Row_Chunk_Size][nodeId]; ++i) {
                    long long val = (col - columnAndDepths[i][row % Row_Chunk_Size][nodeId].column + 1) * (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth - prev_depth);
                    /*{
                        std::unique_lock<std::mutex> lck(mtx, std::defer_lock);
                        lck.lock();
                        std::cout << std::setw(5) << row << "," << std::setw(5) << col << "," << std::setw(5) << val << '\n';
                        lck.unlock();
                    }*/
                    node_result[nodeId] += val;
                    prev_depth = columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth;
                }
            }
        }
    }

    void solve_node_prepare(int nodeId)
    {
        const int Num_Rows = GetFieldHeight();
        const int Num_Cols = GetFieldWidth();

        const int Col_Chunk = Num_Cols / Number_Of_Nodes + 1;
        const int First_Col = nodeId * Col_Chunk;
        const int Bound_Col = std::min((nodeId + 1) * Col_Chunk, Num_Cols);

        int prev_nodeId = nodeId - 1;
        int next_nodeId = nodeId + 1;

        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i][nodeId] = 0;
        }
        for (int row = 0; row < Num_Rows; ++row) {
            //if (row % Row_Chunk_Size == 0) {
            for (int i = First_Col; i < Bound_Col; ++i) {
                depths_initial_for_chunk[i][nodeId] = depths[i][nodeId];
            }
            //}
            min_depth[row % Row_Chunk_Size][nodeId] = std::numeric_limits<int>::max();
            columnAndDepths_len[row % Row_Chunk_Size][nodeId] = 0;
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col][nodeId];
                } else {
                    depths[col][nodeId] = 0;
                }
                int depth = depths[col][nodeId];
                if (min_depth[row % Row_Chunk_Size][nodeId] > depth) {
                    min_depth[row % Row_Chunk_Size][nodeId] = depth;
                }

                int i = columnAndDepths_len[row % Row_Chunk_Size][nodeId] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth > depth) {
                    earliest = columnAndDepths[i][row % Row_Chunk_Size][nodeId].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth < depth) {
                        ++i;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                }
                columnAndDepths_len[row % Row_Chunk_Size][nodeId] = i + 1;
            }

            //if (row % Row_Chunk_Size == Row_Chunk_Size - 1 || row == Num_Rows - 1) {
            if (nodeId > 0) {
                columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId] = GetInt(nodeId, prev_nodeId);
                for (int i = 0; i < columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId]; ++i) {
                    columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].column = GetInt(nodeId, prev_nodeId);
                    columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].depth = GetInt(nodeId, prev_nodeId);
                }
            } else {
                columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId] = 0;
            }
            if (nodeId < Number_Of_Nodes - 1) {
                int receive_le_depth_len = 0;
                for (; receive_le_depth_len < columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId] && columnAndDepths_receive[receive_le_depth_len][row % Row_Chunk_Size][nodeId].depth < min_depth[row % Row_Chunk_Size][nodeId]; ++receive_le_depth_len) {
                }
                int orig_depth = Uninitialized_Negatively;
                if (receive_le_depth_len < columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId]) {
                    orig_depth = columnAndDepths_receive[receive_le_depth_len][row % Row_Chunk_Size][nodeId].depth;
                    columnAndDepths_receive[receive_le_depth_len][row % Row_Chunk_Size][nodeId].depth = min_depth[row % Row_Chunk_Size][nodeId];
                    ++receive_le_depth_len;
                }

                int i_receive = 0;
                int i_i = 0;
                int i_send = 0;
                while (i_receive < receive_le_depth_len || i_i < columnAndDepths_len[row % Row_Chunk_Size][nodeId]) {
                    int column = std::numeric_limits<int>::max();
                    int depth = std::numeric_limits<int>::max();
                    if (i_receive < receive_le_depth_len && i_i < columnAndDepths_len[row % Row_Chunk_Size][nodeId]) {
                        if (columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].depth == columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].depth) {
                            column = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].column;
                            depth = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].depth;
                            ++i_receive;
                            ++i_i;
                        } else if (columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].depth < columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].depth) {
                            column = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].column;
                            depth = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].depth;
                            ++i_receive;
                        } else {
                            column = columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].column;
                            depth = columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].depth;
                            ++i_i;
                        }
                    } else if (i_receive < receive_le_depth_len) {
                        column = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].column;
                        depth = columnAndDepths_receive[i_receive][row % Row_Chunk_Size][nodeId].depth;
                        ++i_receive;
                    } else { // if (i_i < columnAndDepths_len[row % Row_Chunk_Size][nodeId])
                        column = columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].column;
                        depth = columnAndDepths[i_i][row % Row_Chunk_Size][nodeId].depth;
                        ++i_i;
                    }
                    columnAndDepths_send[i_send][row % Row_Chunk_Size][nodeId].column = column;
                    columnAndDepths_send[i_send][row % Row_Chunk_Size][nodeId].depth = depth;
                    ++i_send;
                }
                columnAndDepths_send_len[row % Row_Chunk_Size][nodeId] = i_send;

                if (orig_depth != Uninitialized_Negatively) {
                    columnAndDepths_receive[receive_le_depth_len - 1][row % Row_Chunk_Size][nodeId].depth = orig_depth;
                }

                PutInt(nodeId, next_nodeId, columnAndDepths_send_len[row % Row_Chunk_Size][nodeId]);
                for (int i = 0; i < columnAndDepths_send_len[row % Row_Chunk_Size][nodeId]; ++i) {
                    PutInt(nodeId, next_nodeId, columnAndDepths_send[i][row % Row_Chunk_Size][nodeId].column);
                    PutInt(nodeId, next_nodeId, columnAndDepths_send[i][row % Row_Chunk_Size][nodeId].depth);
                }
            }
            //}

            solve_node_calculate(nodeId, row, row);
        }
    }
    
    long long solve()
    {
        GetFieldHeight();

        std::thread t[Number_Of_Nodes];
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            t[i] = std::thread(solve_node_prepare, i);
        }
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            t[i].join();
        }
        long long result = 0;
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            result += node_result[i];
        }
        return result;
    }
}
#endif



#ifdef V3
#include <disable_warnings.h>
#include <chrono>
#include <thread>
#include <mutex>
#include <iomanip>
#include <reenable_warnings.h>
namespace v3 {
    const int Number_Of_Nodes = 5;
    const int Buf_Size = 1000000;

    int int_buf[Number_Of_Nodes][Number_Of_Nodes][Buf_Size];
    int int_buf_write_to[Number_Of_Nodes][Number_Of_Nodes];
    int int_buf_read_from[Number_Of_Nodes][Number_Of_Nodes];
    void PutInt(int me, int target, int value)
    {
        if (int_buf_write_to[me][target] == Buf_Size) {
            throw std::runtime_error("PutInt");
        }
        int_buf[me][target][int_buf_write_to[me][target]++] = value;
    }
    int GetInt(int me, int source)
    {
        while (int_buf_read_from[source][me] >= int_buf_write_to[source][me]) {
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
        return int_buf[source][me][int_buf_read_from[source][me]++];
    }

    std::mutex mtx;



    using pali::Uninitialized_Negatively;

    const int Side_max = 75000;
    const int Row_Chunk_Size = 100;

    struct ColumnAndDepth
    {
        int column, depth;
    };

    int depths_initial_for_chunk[Side_max][Number_Of_Nodes];
    int depths[Side_max][Number_Of_Nodes];
    int min_depth[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_len[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths_receive[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_receive_len[Row_Chunk_Size][Number_Of_Nodes];
    ColumnAndDepth columnAndDepths_send[Side_max][Row_Chunk_Size][Number_Of_Nodes];
    int columnAndDepths_send_len[Row_Chunk_Size][Number_Of_Nodes];
    long long node_result[Number_Of_Nodes];

    void solve_node_calculate(int nodeId, int from_row, int to_row)
    {
        const int Num_Rows = GetFieldHeight();
        const int Num_Cols = GetFieldWidth();

        const int Col_Chunk = Num_Cols / Number_Of_Nodes + 1;
        const int First_Col = nodeId * Col_Chunk;
        const int Bound_Col = std::min((nodeId + 1) * Col_Chunk, Num_Cols);

        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i][nodeId] = depths_initial_for_chunk[i][nodeId];
        }
        for (int row = from_row; row <= to_row; ++row) {
            for (int i = 0; i < columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId]; ++i) {
                columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].column;
                columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = columnAndDepths_receive[i][row % Row_Chunk_Size][nodeId].depth;
            }
            columnAndDepths_len[row % Row_Chunk_Size][nodeId] = columnAndDepths_receive_len[row % Row_Chunk_Size][nodeId];
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col][nodeId];
                } else {
                    depths[col][nodeId] = 0;
                }
                int depth = depths[col][nodeId];

                int i = columnAndDepths_len[row % Row_Chunk_Size][nodeId] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth > depth) {
                    earliest = columnAndDepths[i][row % Row_Chunk_Size][nodeId].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth < depth) {
                        ++i;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                }
                columnAndDepths_len[row % Row_Chunk_Size][nodeId] = i + 1;

                int prev_depth = 0;
                for (i = 0; i < columnAndDepths_len[row % Row_Chunk_Size][nodeId]; ++i) {
                    long long val = (col - columnAndDepths[i][row % Row_Chunk_Size][nodeId].column + 1) * (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth - prev_depth);
                    /*{
                        std::unique_lock<std::mutex> lck(mtx, std::defer_lock);
                        lck.lock();
                        std::cout << std::setw(5) << row << "," << std::setw(5) << col << "," << std::setw(5) << val << '\n';
                        lck.unlock();
                    }*/
                    node_result[nodeId] += val;
                    prev_depth = columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth;
                }
            }
        }
    }

    void solve_node_prepare(int nodeId)
    {
        const int Num_Rows = GetFieldHeight();
        const int Num_Cols = GetFieldWidth();

        const int Col_Chunk = Num_Cols / Number_Of_Nodes + 1;
        const int First_Col = nodeId * Col_Chunk;
        const int Bound_Col = std::min((nodeId + 1) * Col_Chunk, Num_Cols);

        int prev_nodeId = nodeId - 1;
        int next_nodeId = nodeId + 1;

        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i][nodeId] = 0;
        }
        for (int row = 0; row < Num_Rows; ++row) {
            if (row % Row_Chunk_Size == 0) {
                for (int i = First_Col; i < Bound_Col; ++i) {
                    depths_initial_for_chunk[i][nodeId] = depths[i][nodeId];
                }
            }
            min_depth[row % Row_Chunk_Size][nodeId] = std::numeric_limits<int>::max();
            columnAndDepths_len[row % Row_Chunk_Size][nodeId] = 0;
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col][nodeId];
                } else {
                    depths[col][nodeId] = 0;
                }
                int depth = depths[col][nodeId];
                if (min_depth[row % Row_Chunk_Size][nodeId] > depth) {
                    min_depth[row % Row_Chunk_Size][nodeId] = depth;
                }

                int i = columnAndDepths_len[row % Row_Chunk_Size][nodeId] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth > depth) {
                    earliest = columnAndDepths[i][row % Row_Chunk_Size][nodeId].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth < depth) {
                        ++i;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                        columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].column = earliest;
                    columnAndDepths[i][row % Row_Chunk_Size][nodeId].depth = depth;
                }
                columnAndDepths_len[row % Row_Chunk_Size][nodeId] = i + 1;
            }

            if (row % Row_Chunk_Size == Row_Chunk_Size - 1 || row == Num_Rows - 1) {
                for (int r = 0; r <= row % Row_Chunk_Size; ++r) {
                    if (nodeId > 0) {
                        columnAndDepths_receive_len[r][nodeId] = GetInt(nodeId, prev_nodeId);
                        for (int i = 0; i < columnAndDepths_receive_len[r][nodeId]; ++i) {
                            columnAndDepths_receive[i][r][nodeId].column = GetInt(nodeId, prev_nodeId);
                            columnAndDepths_receive[i][r][nodeId].depth = GetInt(nodeId, prev_nodeId);
                        }
                    } else {
                        columnAndDepths_receive_len[r][nodeId] = 0;
                    }
                }
                if (nodeId < Number_Of_Nodes - 1) {
                    for (int r = 0; r <= row % Row_Chunk_Size; ++r) {
                        int receive_le_depth_len = 0;
                        for (; receive_le_depth_len < columnAndDepths_receive_len[r][nodeId] && columnAndDepths_receive[receive_le_depth_len][r][nodeId].depth < min_depth[r][nodeId]; ++receive_le_depth_len) {
                        }
                        int orig_depth = Uninitialized_Negatively;
                        if (receive_le_depth_len < columnAndDepths_receive_len[r][nodeId]) {
                            orig_depth = columnAndDepths_receive[receive_le_depth_len][r][nodeId].depth;
                            columnAndDepths_receive[receive_le_depth_len][r][nodeId].depth = min_depth[r][nodeId];
                            ++receive_le_depth_len;
                        }

                        int i_receive = 0;
                        int i_i = 0;
                        int i_send = 0;
                        while (i_receive < receive_le_depth_len || i_i < columnAndDepths_len[r][nodeId]) {
                            int column = std::numeric_limits<int>::max();
                            int depth = std::numeric_limits<int>::max();
                            if (i_receive < receive_le_depth_len && i_i < columnAndDepths_len[r][nodeId]) {
                                if (columnAndDepths_receive[i_receive][r][nodeId].depth == columnAndDepths[i_i][r][nodeId].depth) {
                                    column = columnAndDepths_receive[i_receive][r][nodeId].column;
                                    depth = columnAndDepths_receive[i_receive][r][nodeId].depth;
                                    ++i_receive;
                                    ++i_i;
                                } else if (columnAndDepths_receive[i_receive][r][nodeId].depth < columnAndDepths[i_i][r][nodeId].depth) {
                                    column = columnAndDepths_receive[i_receive][r][nodeId].column;
                                    depth = columnAndDepths_receive[i_receive][r][nodeId].depth;
                                    ++i_receive;
                                } else {
                                    column = columnAndDepths[i_i][r][nodeId].column;
                                    depth = columnAndDepths[i_i][r][nodeId].depth;
                                    ++i_i;
                                }
                            } else if (i_receive < receive_le_depth_len) {
                                column = columnAndDepths_receive[i_receive][r][nodeId].column;
                                depth = columnAndDepths_receive[i_receive][r][nodeId].depth;
                                ++i_receive;
                            } else { // if (i_i < columnAndDepths_len[r][nodeId])
                                column = columnAndDepths[i_i][r][nodeId].column;
                                depth = columnAndDepths[i_i][r][nodeId].depth;
                                ++i_i;
                            }
                            columnAndDepths_send[i_send][r][nodeId].column = column;
                            columnAndDepths_send[i_send][r][nodeId].depth = depth;
                            ++i_send;
                        }
                        columnAndDepths_send_len[r][nodeId] = i_send;

                        if (orig_depth != Uninitialized_Negatively) {
                            columnAndDepths_receive[receive_le_depth_len - 1][r][nodeId].depth = orig_depth;
                        }

                        PutInt(nodeId, next_nodeId, columnAndDepths_send_len[r][nodeId]);
                        for (int i = 0; i < columnAndDepths_send_len[r][nodeId]; ++i) {
                            PutInt(nodeId, next_nodeId, columnAndDepths_send[i][r][nodeId].column);
                            PutInt(nodeId, next_nodeId, columnAndDepths_send[i][r][nodeId].depth);
                        }
                    }
                }

                solve_node_calculate(nodeId, row - row % Row_Chunk_Size, row);
            }
        }
    }

    long long solve()
    {
        GetFieldHeight();

        std::thread t[Number_Of_Nodes];
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            t[i] = std::thread(solve_node_prepare, i);
        }
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            t[i].join();
        }
        long long result = 0;
        for (int i = 0; i < Number_Of_Nodes; ++i) {
            result += node_result[i];
        }
        return result;
    }
}
#endif



#ifdef V4
namespace v4 {
    using pali::Uninitialized_Negatively;

    const int Side_max = 75000;
    const int Row_Chunk_Size = 100;

    struct ColumnAndDepth
    {
        int column, depth;
    };

    int depths_initial_for_chunk[Side_max];
    int depths[Side_max];
    int min_depth[Row_Chunk_Size];
    ColumnAndDepth columnAndDepths[Side_max][Row_Chunk_Size];
    int columnAndDepths_len[Row_Chunk_Size];
    ColumnAndDepth columnAndDepths_receive[Side_max][Row_Chunk_Size];
    int columnAndDepths_receive_len[Row_Chunk_Size];
    ColumnAndDepth columnAndDepths_send[Side_max];
    int columnAndDepths_send_len;
    long long node_result;

    int My_Node_Id;
    int Number_Of_Nodes;
    int Num_Rows;
    int Num_Cols;
    int Col_Chunk;
    int First_Col;
    int Bound_Col;

    void solve_node_calculate(int from_row, int to_row)
    {
        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i] = depths_initial_for_chunk[i];
        }
        for (int row = from_row, r = 0; row <= to_row; ++row, ++r) {
            for (int i = 0; i < columnAndDepths_receive_len[r]; ++i) {
                columnAndDepths[i][r].column = columnAndDepths_receive[i][r].column;
                columnAndDepths[i][r].depth = columnAndDepths_receive[i][r].depth;
            }
            columnAndDepths_len[r] = columnAndDepths_receive_len[r];
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col];
                } else {
                    depths[col] = 0;
                }
                int depth = depths[col];

                int i = columnAndDepths_len[r] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][r].depth > depth) {
                    earliest = columnAndDepths[i][r].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][r].depth < depth) {
                        ++i;
                        columnAndDepths[i][r].column = earliest;
                        columnAndDepths[i][r].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][r].column = earliest;
                    columnAndDepths[i][r].depth = depth;
                }
                columnAndDepths_len[r] = i + 1;

                int prev_depth = 0;
                for (i = 0; i < columnAndDepths_len[r]; ++i) {
                    long long val = (col - columnAndDepths[i][r].column + 1) * (columnAndDepths[i][r].depth - prev_depth);
                    node_result += val;
                    prev_depth = columnAndDepths[i][r].depth;
                }
            }
        }
    }

    void solve_node_prepare()
    {
        const int Prev_Node_Id = My_Node_Id - 1;
        const int Next_Node_Id = My_Node_Id + 1;

        for (int i = First_Col; i < Bound_Col; ++i) {
            depths[i] = 0;
        }
        for (int row = 0; row < Num_Rows; ++row) {
            int r = row % Row_Chunk_Size;
            if (r == 0) {
                for (int i = First_Col; i < Bound_Col; ++i) {
                    depths_initial_for_chunk[i] = depths[i];
                }
            }
            min_depth[r] = std::numeric_limits<int>::max();
            columnAndDepths_len[r] = 0;
            for (int col = First_Col; col < Bound_Col; ++col) {
                if (IsUsableCell(row, col)) {
                    ++depths[col];
                } else {
                    depths[col] = 0;
                }
                int depth = depths[col];
                if (min_depth[r] > depth) {
                    min_depth[r] = depth;
                }

                int i = columnAndDepths_len[r] - 1;
                int earliest = col;
                while (i >= 0 && columnAndDepths[i][r].depth > depth) {
                    earliest = columnAndDepths[i][r].column;
                    --i;
                }
                if (i >= 0) {
                    if (columnAndDepths[i][r].depth < depth) {
                        ++i;
                        columnAndDepths[i][r].column = earliest;
                        columnAndDepths[i][r].depth = depth;
                    }
                } else if (depth > 0) {
                    ++i;
                    columnAndDepths[i][r].column = earliest;
                    columnAndDepths[i][r].depth = depth;
                }
                columnAndDepths_len[r] = i + 1;
            }

            if (r == Row_Chunk_Size - 1 || row == Num_Rows - 1) {
                if (My_Node_Id > 0) {
                    Receive(Prev_Node_Id);
                }
                for (int rr = 0; rr <= r; ++rr) {
                    if (My_Node_Id > 0) {
                        columnAndDepths_receive_len[rr] = GetInt(Prev_Node_Id);
                        for (int i = 0; i < columnAndDepths_receive_len[rr]; ++i) {
                            columnAndDepths_receive[i][rr].column = GetInt(Prev_Node_Id);
                            columnAndDepths_receive[i][rr].depth = GetInt(Prev_Node_Id);
                        }
                    } else {
                        columnAndDepths_receive_len[rr] = 0;
                    }
                }
                if (My_Node_Id < Number_Of_Nodes - 1) {
                    for (int rr = 0; rr <= r; ++rr) {
                        int receive_le_depth_len = 0;
                        for (; receive_le_depth_len < columnAndDepths_receive_len[rr] && columnAndDepths_receive[receive_le_depth_len][rr].depth < min_depth[rr]; ++receive_le_depth_len) {
                        }
                        int orig_depth = Uninitialized_Negatively;
                        if (receive_le_depth_len < columnAndDepths_receive_len[rr]) {
                            orig_depth = columnAndDepths_receive[receive_le_depth_len][rr].depth;
                            columnAndDepths_receive[receive_le_depth_len][rr].depth = min_depth[rr];
                            ++receive_le_depth_len;
                        }

                        int i_receive = 0;
                        int i_i = 0;
                        int i_send = 0;
                        while (i_receive < receive_le_depth_len || i_i < columnAndDepths_len[rr]) {
                            int column = std::numeric_limits<int>::max();
                            int depth = std::numeric_limits<int>::max();
                            if (i_receive < receive_le_depth_len && i_i < columnAndDepths_len[rr]) {
                                if (columnAndDepths_receive[i_receive][rr].depth == columnAndDepths[i_i][rr].depth) {
                                    column = columnAndDepths_receive[i_receive][rr].column;
                                    depth = columnAndDepths_receive[i_receive][rr].depth;
                                    ++i_receive;
                                    ++i_i;
                                } else if (columnAndDepths_receive[i_receive][rr].depth < columnAndDepths[i_i][rr].depth) {
                                    column = columnAndDepths_receive[i_receive][rr].column;
                                    depth = columnAndDepths_receive[i_receive][rr].depth;
                                    ++i_receive;
                                } else {
                                    column = columnAndDepths[i_i][rr].column;
                                    depth = columnAndDepths[i_i][rr].depth;
                                    ++i_i;
                                }
                            } else if (i_receive < receive_le_depth_len) {
                                column = columnAndDepths_receive[i_receive][rr].column;
                                depth = columnAndDepths_receive[i_receive][rr].depth;
                                ++i_receive;
                            } else { // if (i_i < columnAndDepths_len[r])
                                column = columnAndDepths[i_i][rr].column;
                                depth = columnAndDepths[i_i][rr].depth;
                                ++i_i;
                            }
                            columnAndDepths_send[i_send].column = column;
                            columnAndDepths_send[i_send].depth = depth;
                            ++i_send;
                        }
                        columnAndDepths_send_len = i_send;

                        if (orig_depth != Uninitialized_Negatively) {
                            columnAndDepths_receive[receive_le_depth_len - 1][rr].depth = orig_depth;
                        }

                        PutInt(Next_Node_Id, columnAndDepths_send_len);
                        for (int i = 0; i < columnAndDepths_send_len; ++i) {
                            PutInt(Next_Node_Id, columnAndDepths_send[i].column);
                            PutInt(Next_Node_Id, columnAndDepths_send[i].depth);
                        }
                    }
                    Send(Next_Node_Id);
                }

                solve_node_calculate(row - r, row);
            }
        }
    }

    void solve()
    {
        GetFieldHeight();

        My_Node_Id = MyNodeId();
        Number_Of_Nodes = NumberOfNodes();
        Num_Rows = GetFieldHeight();
        Num_Cols = GetFieldWidth();
        Col_Chunk = Num_Cols / Number_Of_Nodes + 1;
        First_Col = My_Node_Id * Col_Chunk;
        Bound_Col = std::min((My_Node_Id + 1) * Col_Chunk, Num_Cols);

        solve_node_prepare();
        if (My_Node_Id == 0) {
            long long result = node_result;
            for (int i = 1; i < Number_Of_Nodes; ++i) {
                Receive(i);
                result += GetLL(i);
            }
            std::cout << result << '\n';
        } else {
            PutLL(0, node_result);
            Send(0);
        }
    }
}
#endif



int main()
{
    v4::solve();
    //long long result = v4::solve();
    //std::cout << result << "\n";
}